結果

問題 No.458 異なる素数の和
ユーザー neterukunneterukun
提出日時 2019-06-03 03:05:15
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,094 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 618,224 KB
最終ジャッジ日時 2023-10-17 23:08:53
合計ジャッジ時間 9,657 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
64,224 KB
testcase_01 AC 479 ms
276,780 KB
testcase_02 AC 568 ms
304,004 KB
testcase_03 AC 150 ms
112,856 KB
testcase_04 AC 182 ms
132,956 KB
testcase_05 MLE -
testcase_06 AC 559 ms
316,400 KB
testcase_07 AC 56 ms
66,340 KB
testcase_08 MLE -
testcase_09 AC 86 ms
82,532 KB
testcase_10 AC 38 ms
53,460 KB
testcase_11 MLE -
testcase_12 AC 38 ms
53,692 KB
testcase_13 AC 38 ms
53,692 KB
testcase_14 AC 38 ms
53,692 KB
testcase_15 AC 38 ms
53,692 KB
testcase_16 AC 105 ms
88,156 KB
testcase_17 AC 43 ms
59,972 KB
testcase_18 AC 42 ms
59,972 KB
testcase_19 AC 38 ms
53,692 KB
testcase_20 AC 45 ms
60,132 KB
testcase_21 AC 39 ms
53,692 KB
testcase_22 AC 38 ms
53,692 KB
testcase_23 AC 44 ms
59,988 KB
testcase_24 AC 45 ms
60,132 KB
testcase_25 AC 38 ms
53,692 KB
testcase_26 AC 42 ms
59,972 KB
testcase_27 AC 537 ms
311,708 KB
testcase_28 MLE -
testcase_29 AC 67 ms
72,808 KB
testcase_30 AC 344 ms
205,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def make_prime_numbers(n):
    '''
    n以下の素数を列挙したリストを出力する
    計算量:O(NloglogN)
    入出力例:30 -> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    '''

    is_prime = [True]*(n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(2 * i, n + 1, i):
            is_prime[j] = False
    prime_numbers = [i for i in range(n + 1) if is_prime[i]]
    return prime_numbers

n = int(input())
prime_list = make_prime_numbers(n)
# dp[i][j] := i個めまでの素数を選んだときに値がjになるとき選んだ個数の最大値
dp = [[-float("inf")]*(n+1) for i in range(len(prime_list) + 1)]

for i in range(len(prime_list)):
    dp[i][0] = 0
for i in range(len(prime_list)):
    for j in range(n+1):
        if prime_list[i] > j:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i][j-prime_list[i]] + 1)
if dp[len(prime_list)][n] == -float("inf"):
    print(-1)
else:
    print(dp[len(prime_list)][n])
0