結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
75,868 KB
testcase_01 AC 300 ms
78,544 KB
testcase_02 AC 366 ms
78,904 KB
testcase_03 AC 138 ms
77,232 KB
testcase_04 AC 150 ms
77,428 KB
testcase_05 AC 760 ms
180,092 KB
testcase_06 AC 349 ms
78,932 KB
testcase_07 AC 81 ms
75,980 KB
testcase_08 AC 770 ms
181,156 KB
testcase_09 AC 103 ms
76,992 KB
testcase_10 AC 72 ms
71,204 KB
testcase_11 AC 875 ms
187,776 KB
testcase_12 AC 73 ms
71,152 KB
testcase_13 AC 72 ms
71,236 KB
testcase_14 AC 72 ms
71,184 KB
testcase_15 AC 72 ms
71,108 KB
testcase_16 AC 119 ms
77,128 KB
testcase_17 AC 73 ms
71,428 KB
testcase_18 AC 75 ms
71,468 KB
testcase_19 AC 75 ms
71,292 KB
testcase_20 AC 73 ms
71,108 KB
testcase_21 AC 72 ms
71,228 KB
testcase_22 AC 74 ms
71,308 KB
testcase_23 AC 73 ms
71,476 KB
testcase_24 AC 73 ms
71,212 KB
testcase_25 AC 74 ms
71,440 KB
testcase_26 AC 74 ms
71,472 KB
testcase_27 AC 336 ms
78,744 KB
testcase_28 AC 863 ms
184,316 KB
testcase_29 AC 96 ms
76,304 KB
testcase_30 AC 241 ms
77,988 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