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