結果

問題 No.458 異なる素数の和
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-10 14:28:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 631 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 76,704 KB
最終ジャッジ日時 2024-05-01 00:38:32
合計ジャッジ時間 4,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
59,832 KB
testcase_01 AC 138 ms
76,320 KB
testcase_02 AC 166 ms
76,320 KB
testcase_03 AC 75 ms
76,268 KB
testcase_04 AC 81 ms
76,416 KB
testcase_05 AC 292 ms
76,388 KB
testcase_06 AC 161 ms
76,320 KB
testcase_07 AC 47 ms
61,064 KB
testcase_08 AC 293 ms
76,356 KB
testcase_09 AC 62 ms
68,864 KB
testcase_10 AC 42 ms
52,352 KB
testcase_11 AC 340 ms
76,512 KB
testcase_12 AC 40 ms
53,124 KB
testcase_13 AC 40 ms
52,776 KB
testcase_14 AC 39 ms
53,216 KB
testcase_15 AC 40 ms
53,072 KB
testcase_16 AC 67 ms
76,260 KB
testcase_17 AC 41 ms
52,736 KB
testcase_18 AC 40 ms
52,608 KB
testcase_19 AC 39 ms
52,736 KB
testcase_20 AC 44 ms
58,240 KB
testcase_21 AC 40 ms
52,992 KB
testcase_22 AC 40 ms
52,608 KB
testcase_23 AC 45 ms
58,688 KB
testcase_24 AC 44 ms
58,368 KB
testcase_25 AC 42 ms
52,480 KB
testcase_26 AC 41 ms
52,736 KB
testcase_27 AC 153 ms
76,288 KB
testcase_28 AC 332 ms
76,704 KB
testcase_29 AC 55 ms
65,536 KB
testcase_30 AC 116 ms
76,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import chain
inf = 10**18


def prime_set(N):
    """
    Nまでの素数のsetを返す
    """
    if N < 4:
        return ({}, {}, {2}, {2, 3})[N]
    Nsq = int(N ** 0.5 + 0.5) + 1
    primes = {2, 3} | set(chain(range(5, N + 1, 6), range(7, N + 1, 6)))
    for i in range(5, Nsq, 2):
        if i in primes:
            primes -= set(range(i * i, N + 1, i * 2))
    return primes


N = int(input())
prime = sorted(prime_set(N))

dp = [-inf] * (N + 1)
dp[0] = 0
for p in prime:
    for i in reversed(range(p, N + 1)):
        dp[i] = max(dp[i], dp[i - p] + 1)

if dp[N] < 0:
    print(-1)
else:
    print(dp[N])
0