結果

問題 No.458 異なる素数の和
ユーザー neko_the_shadowneko_the_shadow
提出日時 2017-01-15 13:37:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 880 ms / 2,000 ms
コード長 565 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 188,616 KB
最終ジャッジ日時 2023-09-18 15:16:35
合計ジャッジ時間 8,282 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
75,920 KB
testcase_01 AC 302 ms
78,672 KB
testcase_02 AC 363 ms
79,312 KB
testcase_03 AC 137 ms
77,412 KB
testcase_04 AC 150 ms
77,484 KB
testcase_05 AC 753 ms
180,092 KB
testcase_06 AC 347 ms
79,000 KB
testcase_07 AC 82 ms
76,404 KB
testcase_08 AC 760 ms
181,320 KB
testcase_09 AC 100 ms
77,324 KB
testcase_10 AC 72 ms
71,244 KB
testcase_11 AC 880 ms
187,584 KB
testcase_12 AC 74 ms
70,984 KB
testcase_13 AC 72 ms
71,416 KB
testcase_14 AC 74 ms
71,200 KB
testcase_15 AC 71 ms
71,472 KB
testcase_16 AC 117 ms
77,460 KB
testcase_17 AC 74 ms
71,248 KB
testcase_18 AC 75 ms
71,072 KB
testcase_19 AC 72 ms
71,136 KB
testcase_20 AC 74 ms
71,200 KB
testcase_21 AC 77 ms
71,140 KB
testcase_22 AC 73 ms
71,284 KB
testcase_23 AC 73 ms
71,212 KB
testcase_24 AC 74 ms
70,980 KB
testcase_25 AC 75 ms
71,236 KB
testcase_26 AC 73 ms
71,360 KB
testcase_27 AC 333 ms
78,960 KB
testcase_28 AC 866 ms
188,616 KB
testcase_29 AC 90 ms
76,716 KB
testcase_30 AC 238 ms
78,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

if __name__ == '__main__':
    n = int(input())

    primes = list(range(n + 1))
    primes[0] = primes[1] = None

    ht = {0 : 0}
    for prime in primes:
        if prime is None: continue
        if prime <= math.sqrt(n):
            for non_prime in range(prime * 2, n + 1, prime): primes[non_prime] = None

        temp = {}
        for ky in sorted(ht):
            x = ky + prime
            if x > n: break
            if not x in ht or ht[x] < ht[ky] + 1: temp[x] = ht[ky] + 1
        ht.update(temp)
    
    print(ht[n] if n in ht else -1)
0