結果

問題 No.458 異なる素数の和
ユーザー nanaenanae
提出日時 2017-03-18 02:57:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 668 bytes
コンパイル時間 776 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 76,876 KB
最終ジャッジ日時 2023-09-18 15:24:33
合計ジャッジ時間 5,605 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
75,496 KB
testcase_01 AC 138 ms
76,372 KB
testcase_02 AC 158 ms
76,484 KB
testcase_03 AC 98 ms
76,412 KB
testcase_04 AC 102 ms
76,516 KB
testcase_05 AC 242 ms
76,832 KB
testcase_06 AC 153 ms
76,288 KB
testcase_07 AC 82 ms
75,476 KB
testcase_08 AC 240 ms
76,668 KB
testcase_09 AC 90 ms
76,372 KB
testcase_10 AC 77 ms
71,428 KB
testcase_11 AC 273 ms
76,876 KB
testcase_12 AC 77 ms
71,396 KB
testcase_13 AC 76 ms
71,280 KB
testcase_14 AC 77 ms
71,220 KB
testcase_15 AC 76 ms
71,328 KB
testcase_16 AC 92 ms
76,308 KB
testcase_17 AC 78 ms
71,508 KB
testcase_18 AC 76 ms
71,140 KB
testcase_19 AC 77 ms
71,476 KB
testcase_20 AC 80 ms
75,236 KB
testcase_21 AC 75 ms
71,076 KB
testcase_22 AC 75 ms
71,288 KB
testcase_23 AC 78 ms
75,536 KB
testcase_24 AC 78 ms
75,404 KB
testcase_25 AC 77 ms
71,452 KB
testcase_26 AC 76 ms
71,428 KB
testcase_27 AC 148 ms
76,552 KB
testcase_28 AC 270 ms
76,720 KB
testcase_29 AC 86 ms
76,292 KB
testcase_30 AC 125 ms
76,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin, stdout, stderr

def solve():
    N = int(input())
    ps = make_primes(N + 1)

    minf = -(len(ps) + 1)
    dp = [minf] * (N + 1)
    dp[0] = 0

    for p in ps:
        for i in range(N, p - 1, -1):
            dp[i] = max(dp[i], dp[i - p] + 1)

    # print(dp)
    ans = dp[N]

    if ans > 0:
        print(ans)
    else:
        print(-1)


def make_primes(N):
    sieve = [True] * N
    sieve[0] = sieve[1] = False

    for p in range(2, N):
        if not sieve[p]:
            continue

        for m in range(p**2, N, p):
            sieve[m] = False

    return [i for i in range(N) if sieve[i]]

if __name__ == '__main__':
    solve()
0