結果
| 問題 |
No.458 異なる素数の和
|
| コンテスト | |
| ユーザー |
jj
|
| 提出日時 | 2016-12-09 00:20:33 |
| 言語 | Fortran (gFortran 14.2.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 2,000 ms |
| コード長 | 1,182 bytes |
| コンパイル時間 | 1,787 ms |
| コンパイル使用メモリ | 34,800 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-05 05:36:16 |
| 合計ジャッジ時間 | 2,754 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
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
jj