結果

問題 No.458 異なる素数の和
ユーザー Yuu EguciYuu Eguci
提出日時 2020-12-05 14:03:55
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 933 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 525,632 KB
最終ジャッジ日時 2023-10-13 21:33:16
合計ジャッジ時間 10,763 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
75,756 KB
testcase_01 AC 385 ms
178,840 KB
testcase_02 AC 486 ms
215,644 KB
testcase_03 AC 149 ms
93,356 KB
testcase_04 AC 171 ms
105,488 KB
testcase_05 AC 1,070 ms
465,768 KB
testcase_06 AC 624 ms
215,568 KB
testcase_07 AC 85 ms
75,628 KB
testcase_08 AC 994 ms
468,260 KB
testcase_09 AC 109 ms
81,364 KB
testcase_10 AC 72 ms
71,288 KB
testcase_11 MLE -
testcase_12 AC 83 ms
71,196 KB
testcase_13 WA -
testcase_14 AC 73 ms
71,272 KB
testcase_15 AC 77 ms
71,192 KB
testcase_16 AC 126 ms
82,068 KB
testcase_17 AC 78 ms
75,072 KB
testcase_18 AC 79 ms
75,340 KB
testcase_19 AC 73 ms
71,080 KB
testcase_20 AC 81 ms
75,604 KB
testcase_21 AC 76 ms
71,348 KB
testcase_22 AC 76 ms
71,284 KB
testcase_23 AC 79 ms
75,600 KB
testcase_24 AC 83 ms
75,620 KB
testcase_25 AC 75 ms
71,004 KB
testcase_26 AC 78 ms
75,280 KB
testcase_27 AC 454 ms
203,580 KB
testcase_28 MLE -
testcase_29 AC 95 ms
76,028 KB
testcase_30 AC 326 ms
154,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def makePrimes(n: int) -> list:
    sieve = [True] * (n + 1)
    p = 2
    c = 0
    while p * p <= n:
        if sieve[p]:
            for i in range(p * 2, n + 1, p):
                sieve[i] = False
        p += 1
    for i in range(2, n + 1):
        if sieve[i]:
            yield i


def dynamic_knapsack(constrain: int) -> int:
    # dp = [[0 if i == 0 else -1 for i in range(constrain+1)] for j in range(constrain+1)]
    nn = range(constrain + 1)
    dp = [[0] + [-1] * constrain]
    add_dp = dp.append
    primes = makePrimes(constrain)
    i = 0
    for p in primes:
        i += 1
        add_dp([0] + [-1] * constrain)
        for w in nn:
            dp[i][w] = max(1 + dp[i - 1][w - p], dp[i - 1][w]
                           ) if p <= w and dp[i - 1][w - p] != -1 else dp[i - 1][w]
    return dp[i - 1][constrain]


if __name__ == "__main__":
    n = int(input())
    result = dynamic_knapsack(n)
    print(result)
0