結果
問題 |
No.458 異なる素数の和
|
ユーザー |
|
提出日時 | 2019-01-26 12:10:36 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 160 ms / 2,000 ms |
コード長 | 1,054 bytes |
コンパイル時間 | 2,532 ms |
コンパイル使用メモリ | 61,000 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 10:53:21 |
合計ジャッジ時間 | 4,212 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
import sequtils,math template `max=`*(x,y) = x = max(x,y) template `min=`*(x,y) = x = min(x,y) proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': break result = 10 * result + k.ord - '0'.ord proc getIsPrimes(n:int) :seq[bool] = # [0...n] O(n loglog n) result = newSeqWith(n+1,true) result[0] = false result[1] = false for i in 2..n.float.sqrt.int : if not result[i]: continue for j in countup(i*2,n,i): result[j] = false proc getPrimes(n:int):seq[int] = # [2,3,5,...n] let isPrimes = getIsPrimes(n) result = @[] for i,p in isPrimes: if p : result &= i const primes = getPrimes(20010) var dp: array[20010,int] let n = scan() var maxN = 0 for p in primes: for i in maxN.countdown(2): if i > n or i + p > n : continue if dp[i] == 0 : continue dp[i+p] .max= dp[i] + 1 maxN .max= i + p maxN .min= n dp[p] .max= 1 maxN .max= p maxN .min= n if dp[n] == 0 : echo -1 else: echo dp[n]