結果

問題 No.458 異なる素数の和
ユーザー ireenaireena
提出日時 2022-01-22 15:28:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 581 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 87,332 KB
実行使用メモリ 76,904 KB
最終ジャッジ日時 2023-08-18 08:52:59
合計ジャッジ時間 5,215 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
76,032 KB
testcase_01 AC 138 ms
76,488 KB
testcase_02 AC 159 ms
76,352 KB
testcase_03 AC 93 ms
76,576 KB
testcase_04 AC 97 ms
76,360 KB
testcase_05 AC 247 ms
76,836 KB
testcase_06 AC 152 ms
76,520 KB
testcase_07 AC 76 ms
76,012 KB
testcase_08 AC 252 ms
76,840 KB
testcase_09 AC 86 ms
76,556 KB
testcase_10 AC 71 ms
71,108 KB
testcase_11 AC 288 ms
76,904 KB
testcase_12 AC 69 ms
71,452 KB
testcase_13 AC 71 ms
71,396 KB
testcase_14 AC 71 ms
71,248 KB
testcase_15 AC 71 ms
71,288 KB
testcase_16 AC 90 ms
76,576 KB
testcase_17 AC 69 ms
71,288 KB
testcase_18 AC 70 ms
71,240 KB
testcase_19 AC 69 ms
71,420 KB
testcase_20 AC 74 ms
75,864 KB
testcase_21 AC 71 ms
71,284 KB
testcase_22 AC 72 ms
71,244 KB
testcase_23 AC 74 ms
75,632 KB
testcase_24 AC 74 ms
75,776 KB
testcase_25 AC 71 ms
71,532 KB
testcase_26 AC 72 ms
71,312 KB
testcase_27 AC 151 ms
76,408 KB
testcase_28 AC 279 ms
76,752 KB
testcase_29 AC 81 ms
76,552 KB
testcase_30 AC 120 ms
76,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
INF = 1 << 30

def list_primes(limit):
    primes = []
    is_prime = [True] * (limit + 1)
    is_prime[0] = False
    is_prime[1] = False

    for p in range (0, limit + 1):
        if not is_prime[p]:
            continue
        primes.append(p)
        for i in range(p*p, limit + 1, p):
            is_prime[i] = False

    return primes

primes = list_primes(N)

dp = [-INF] * (N+1)
dp[0] = 0

for p in primes:
    for i in range(N+1)[::-1]:
        if i - p < 0:
            break
        dp[i] = max(dp[i], dp[i-p] + 1)

print(dp[-1] if dp[-1] > 0 else -1)
0