結果

問題 No.458 異なる素数の和
ユーザー h_nosonh_noson
提出日時 2017-01-26 12:18:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 675 ms / 2,000 ms
コード長 582 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 78,796 KB
最終ジャッジ日時 2023-08-24 23:54:28
合計ジャッジ時間 8,263 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
77,000 KB
testcase_01 AC 286 ms
77,812 KB
testcase_02 AC 332 ms
77,856 KB
testcase_03 AC 162 ms
77,932 KB
testcase_04 AC 172 ms
77,832 KB
testcase_05 AC 577 ms
78,796 KB
testcase_06 AC 327 ms
77,924 KB
testcase_07 AC 132 ms
77,796 KB
testcase_08 AC 592 ms
78,544 KB
testcase_09 AC 140 ms
78,188 KB
testcase_10 AC 102 ms
72,264 KB
testcase_11 AC 675 ms
78,428 KB
testcase_12 AC 104 ms
72,056 KB
testcase_13 AC 103 ms
72,196 KB
testcase_14 AC 108 ms
72,196 KB
testcase_15 AC 109 ms
72,420 KB
testcase_16 AC 157 ms
78,300 KB
testcase_17 AC 112 ms
76,920 KB
testcase_18 AC 113 ms
76,704 KB
testcase_19 AC 106 ms
72,284 KB
testcase_20 AC 116 ms
76,832 KB
testcase_21 AC 109 ms
72,448 KB
testcase_22 AC 108 ms
72,372 KB
testcase_23 AC 108 ms
76,860 KB
testcase_24 AC 109 ms
76,840 KB
testcase_25 AC 104 ms
72,332 KB
testcase_26 AC 106 ms
76,676 KB
testcase_27 AC 306 ms
78,084 KB
testcase_28 AC 653 ms
78,608 KB
testcase_29 AC 133 ms
78,136 KB
testcase_30 AC 246 ms
77,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import reduce

def primes(n):
    is_p = [True for i in range(n+1)]
    i = 2
    while i * i <= n:
        if is_p[i]:
            for j in range(2*i,n+1,i):
                is_p[j] = False
        i += 1
    return [i for i in range(2,n+1) if is_p[i]]

n = int(input())
p = primes(n)
mp = {0:0}
dp = [[-1 for i in range(n+1)] for j in range(2)]
dp[0][0] = 0
ans = -1
for i in range(len(p)):
    for j in range(n+1):
        dp[(i+1)%2][j] = max(dp[i%2][j],dp[i%2][j-p[i]]+1 if j >= p[i] and dp[i%2][j-p[i]] >= 0 else -1)
    ans = max(ans,dp[(i+1)%2][n])
print(ans)
0