結果

問題 No.458 異なる素数の和
ユーザー yakeshibayakeshiba
提出日時 2021-03-11 03:57:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,435 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 423,168 KB
最終ジャッジ日時 2024-04-20 20:36:41
合計ジャッジ時間 10,790 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
64,256 KB
testcase_01 AC 475 ms
176,768 KB
testcase_02 AC 578 ms
201,600 KB
testcase_03 AC 154 ms
89,892 KB
testcase_04 AC 197 ms
104,960 KB
testcase_05 AC 1,151 ms
362,752 KB
testcase_06 AC 632 ms
200,576 KB
testcase_07 AC 65 ms
67,200 KB
testcase_08 AC 1,159 ms
365,440 KB
testcase_09 AC 92 ms
82,944 KB
testcase_10 AC 38 ms
52,096 KB
testcase_11 AC 1,435 ms
423,168 KB
testcase_12 AC 44 ms
52,608 KB
testcase_13 AC 37 ms
52,096 KB
testcase_14 AC 37 ms
52,480 KB
testcase_15 AC 45 ms
52,608 KB
testcase_16 AC 146 ms
88,704 KB
testcase_17 AC 45 ms
59,904 KB
testcase_18 AC 41 ms
58,624 KB
testcase_19 AC 35 ms
52,224 KB
testcase_20 AC 46 ms
61,184 KB
testcase_21 AC 44 ms
51,968 KB
testcase_22 AC 38 ms
52,608 KB
testcase_23 AC 53 ms
59,904 KB
testcase_24 AC 42 ms
60,416 KB
testcase_25 AC 37 ms
52,736 KB
testcase_26 AC 40 ms
58,496 KB
testcase_27 AC 573 ms
194,048 KB
testcase_28 AC 1,322 ms
415,616 KB
testcase_29 AC 77 ms
72,344 KB
testcase_30 AC 354 ms
148,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def create_prime_list(limit):
    x = limit**0.5
    primes = []
    #print('x={0}'.format(x))
    nums = [x for x in range(2, limit+1)]
    while nums[0]<=x:
        primes.append(nums[0])
        current_prime = nums[0]
        nums = [x for x in nums if x%current_prime != 0]
    primes.extend(nums)
    return primes

n = int(input())

if n == 1:
    print(-1)
    exit()

plist = create_prime_list(n)

dp = [[-1]*(n+1) for _ in range(len(plist))]

for i,p in enumerate(plist):
    dp[i][p] = 1

for i in range(len(plist)):
    for S in range(1,n+1):
        if dp[i-1][S] >= 1:
            dp[i][S] = max(dp[i-1][S], dp[i][S])
        if S-plist[i] >= 0 and dp[i-1][S-plist[i]] >= 1:
            dp[i][S] = max(dp[i-1][S-plist[i]]+1, dp[i][S])

print(dp[len(plist)-1][n])
0