結果

問題 No.458 異なる素数の和
ユーザー juris_lazyjuris_lazy
提出日時 2022-08-04 13:36:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,144 ms / 2,000 ms
コード長 654 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 259,968 KB
最終ジャッジ日時 2023-10-12 11:03:45
合計ジャッジ時間 10,199 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
76,416 KB
testcase_01 AC 381 ms
79,384 KB
testcase_02 AC 455 ms
80,720 KB
testcase_03 AC 167 ms
77,808 KB
testcase_04 AC 184 ms
77,796 KB
testcase_05 AC 996 ms
259,556 KB
testcase_06 AC 441 ms
80,348 KB
testcase_07 AC 97 ms
76,144 KB
testcase_08 AC 1,002 ms
259,968 KB
testcase_09 AC 122 ms
77,560 KB
testcase_10 AC 74 ms
71,368 KB
testcase_11 AC 1,144 ms
259,648 KB
testcase_12 AC 74 ms
71,224 KB
testcase_13 AC 72 ms
71,424 KB
testcase_14 AC 74 ms
71,176 KB
testcase_15 AC 73 ms
71,004 KB
testcase_16 AC 140 ms
77,672 KB
testcase_17 AC 77 ms
75,968 KB
testcase_18 AC 78 ms
76,352 KB
testcase_19 AC 72 ms
71,148 KB
testcase_20 AC 83 ms
76,256 KB
testcase_21 AC 73 ms
71,368 KB
testcase_22 AC 74 ms
71,224 KB
testcase_23 AC 78 ms
76,268 KB
testcase_24 AC 82 ms
76,184 KB
testcase_25 AC 74 ms
71,272 KB
testcase_26 AC 77 ms
75,968 KB
testcase_27 AC 419 ms
79,468 KB
testcase_28 AC 1,128 ms
259,680 KB
testcase_29 AC 112 ms
77,396 KB
testcase_30 AC 298 ms
79,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False

    for i in range(2, n+1):
        if is_prime[i-1]:
            j = 2 * i
            while j <= n:
                is_prime[j-1] = False
                j += i
    table = [ i for i in range(1, n+1) if is_prime[i-1]]
    return table

N = int(input())
table = sieve(N)

dp = [-float('inf')] * (N + 1)
dp[0] = 0
for i in range(len(table)):
  a  = table[i]
  nxt = [-float('inf')] * (N + 1)
  for j in range(N + 1):
    nxt[j] = max(nxt[j], dp[j])
    if j + a <= N:
      nxt[j + a] = max(nxt[j + a], dp[j] + 1)
  dp = nxt
if dp[-1] < 0:
  print(-1)
else:
  print(dp[-1])
0