結果

問題 No.458 異なる素数の和
ユーザー brthyyjpbrthyyjp
提出日時 2021-12-18 16:53:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 893 ms / 2,000 ms
コード長 694 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 77,420 KB
最終ジャッジ日時 2023-10-13 17:59:07
合計ジャッジ時間 9,611 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
75,880 KB
testcase_01 AC 317 ms
77,004 KB
testcase_02 AC 374 ms
77,088 KB
testcase_03 AC 132 ms
76,956 KB
testcase_04 AC 145 ms
76,988 KB
testcase_05 AC 742 ms
77,232 KB
testcase_06 AC 369 ms
77,096 KB
testcase_07 AC 78 ms
76,000 KB
testcase_08 AC 785 ms
77,040 KB
testcase_09 AC 92 ms
77,024 KB
testcase_10 AC 63 ms
71,372 KB
testcase_11 AC 878 ms
77,296 KB
testcase_12 AC 67 ms
71,416 KB
testcase_13 AC 64 ms
71,308 KB
testcase_14 AC 65 ms
71,592 KB
testcase_15 AC 65 ms
71,636 KB
testcase_16 AC 103 ms
77,208 KB
testcase_17 AC 70 ms
75,596 KB
testcase_18 AC 68 ms
75,772 KB
testcase_19 AC 70 ms
71,528 KB
testcase_20 AC 69 ms
75,820 KB
testcase_21 AC 64 ms
71,688 KB
testcase_22 AC 67 ms
71,420 KB
testcase_23 AC 68 ms
75,788 KB
testcase_24 AC 69 ms
75,868 KB
testcase_25 AC 65 ms
71,336 KB
testcase_26 AC 66 ms
75,560 KB
testcase_27 AC 347 ms
77,084 KB
testcase_28 AC 893 ms
77,420 KB
testcase_29 AC 82 ms
76,408 KB
testcase_30 AC 240 ms
76,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def sieve(n):
    ass = []
    is_prime = [True]*(n+1)
    is_prime[0] = False
    is_prime[1] = False

    for i in range(2, int(math.sqrt(n))+1):
        if not is_prime[i]:
            continue
        for j in range(i*2, n+1, i):
            is_prime[j] = False
    for i in range(n+1):
        if is_prime[i]:
            ass.append(i)
    return(ass)

def main():

    n = int(input())
    P = sieve(n)
    dp = [-1]*(n+1)
    dp[0] = 0
    for p in P:
        for i in reversed(range(n+1)):
            if dp[i] == -1:
                continue
            if i+p <= n:
                dp[i+p] = max(dp[i+p], dp[i]+1)

    print(dp[n])

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