結果

問題 No.458 異なる素数の和
ユーザー rlangevinrlangevin
提出日時 2023-02-27 12:50:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 917 ms / 2,000 ms
コード長 669 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 260,184 KB
最終ジャッジ日時 2023-10-12 22:50:58
合計ジャッジ時間 8,758 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
76,136 KB
testcase_01 AC 316 ms
78,252 KB
testcase_02 AC 357 ms
78,844 KB
testcase_03 AC 132 ms
77,348 KB
testcase_04 AC 145 ms
77,408 KB
testcase_05 AC 802 ms
259,856 KB
testcase_06 AC 339 ms
78,736 KB
testcase_07 AC 81 ms
76,088 KB
testcase_08 AC 803 ms
260,184 KB
testcase_09 AC 100 ms
76,768 KB
testcase_10 AC 71 ms
71,240 KB
testcase_11 AC 917 ms
260,008 KB
testcase_12 AC 71 ms
71,240 KB
testcase_13 AC 71 ms
71,176 KB
testcase_14 AC 69 ms
71,460 KB
testcase_15 AC 70 ms
71,408 KB
testcase_16 AC 112 ms
77,324 KB
testcase_17 AC 74 ms
75,704 KB
testcase_18 AC 75 ms
75,848 KB
testcase_19 AC 70 ms
71,580 KB
testcase_20 AC 77 ms
76,000 KB
testcase_21 AC 71 ms
71,524 KB
testcase_22 AC 69 ms
71,324 KB
testcase_23 AC 75 ms
76,016 KB
testcase_24 AC 75 ms
75,852 KB
testcase_25 AC 74 ms
75,648 KB
testcase_26 AC 75 ms
75,736 KB
testcase_27 AC 324 ms
78,176 KB
testcase_28 AC 908 ms
260,028 KB
testcase_29 AC 95 ms
76,700 KB
testcase_30 AC 235 ms
77,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import sqrt, ceil

def Sieve(n):
    lst = [True] * (n + 1)
    S = set()
    for i in range(2, ceil(sqrt(n)) + 1):
        if lst[i]:
            for j in range(2 * i, n + 1, i):
                lst[j] = False
    for i in range(2, n + 1):
        if lst[i]:
            S.add(i)
    return sorted(list(S))

N = int(input())
P = Sieve(N + 100)
M = len(P)
inf = 10 ** 18
pre = [-inf] * (N + 1)
pre[0] = 0
for i in range(M):
    dp = [-inf] * (N + 1)
    for j in range(N + 1):
        dp[j] = pre[j]
        if j - P[i] < 0:
            continue
        dp[j] = max(dp[j], pre[j - P[i]] + 1)
    dp, pre = pre, dp

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