結果

問題 No.458 異なる素数の和
ユーザー むらため
提出日時 2019-01-26 12:05:12
言語 Nim
(2.2.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 900 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 61,072 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 10:53:17
合計ジャッジ時間 3,831 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,math
template `max=`*(x,y) = x = max(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()
for p in primes:
  for i in (n-p).countdown(2):
    if dp[i] == 0 : continue
    dp[i+p] .max= dp[i] + 1
  dp[p] .max= 1

if dp[n] == 0 : echo -1
else: echo dp[n]
0