結果

問題 No.458 異なる素数の和
ユーザー jjjj
提出日時 2016-12-09 00:20:33
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,182 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 30,012 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 14:51:56
合計ジャッジ時間 1,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 7 ms
4,376 KB
testcase_02 AC 9 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 19 ms
4,380 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 18 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 8 ms
4,376 KB
testcase_28 AC 22 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

program main
  implicit none
  integer*8::N,i,j,prime,total
  integer*8,allocatable::PrimeList(:)
  integer*8,allocatable::dp(:)
  read *,N
  allocate(dp(1:N))
  dp = 0
  total = 0

  call get_prime(PrimeList, N)
  do i=1 ,size(PrimeList)
     prime = PrimeList(i)
     total = prime + total
     do j=MIN(N-prime,total),1,-1
        if(dp(j) /= 0) then
           dp(j+prime) = MAX(dp(j) + 1,dp(j+prime))
        end if
     end do
     if(dp(prime) == 0) then
        dp(prime) = 1
     end if
  end do
  if(dp(N)==0) dp(N)=-1
  print '(i0)',dp(N)

contains
  subroutine get_prime(PrimeList, N)
    implicit none
    integer*8::i,j,N
    integer*8,parameter::prime=1,nonprime=0
    integer*8,allocatable::isPrime(:), PrimeList(:)
    integer*8,allocatable::numlist(:)

    allocate(numlist(N))
    allocate(isPrime(N))

    isPrime=prime
    isPrime(1)=nonprime
    isPrime(2)=prime

    do i=2, FLOOR(SQRT(REAL(N)))
       if(isPrime(i).eq.prime) then
          do j=i+i, N, i
             isPrime(j) = nonprime
          end do
       end if
    end do

    numlist=(/(i,i=1,N)/)
    PrimeList=pack(numlist,isPrime(numlist).eq.prime)
  end subroutine get_prime
end program main
0