結果

問題 No.458 異なる素数の和
ユーザー 👑 KazunKazun
提出日時 2021-02-14 17:14:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 77,280 KB
最終ジャッジ日時 2023-09-29 08:22:47
合計ジャッジ時間 5,438 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
76,280 KB
testcase_01 AC 149 ms
76,748 KB
testcase_02 AC 167 ms
76,544 KB
testcase_03 AC 100 ms
76,504 KB
testcase_04 AC 102 ms
76,624 KB
testcase_05 AC 263 ms
77,096 KB
testcase_06 AC 163 ms
76,800 KB
testcase_07 AC 88 ms
76,436 KB
testcase_08 AC 263 ms
77,140 KB
testcase_09 AC 94 ms
76,672 KB
testcase_10 AC 76 ms
71,356 KB
testcase_11 AC 297 ms
77,280 KB
testcase_12 AC 75 ms
71,408 KB
testcase_13 AC 77 ms
71,468 KB
testcase_14 AC 76 ms
70,976 KB
testcase_15 AC 74 ms
71,412 KB
testcase_16 AC 96 ms
76,692 KB
testcase_17 AC 73 ms
71,468 KB
testcase_18 AC 72 ms
70,972 KB
testcase_19 AC 80 ms
71,460 KB
testcase_20 AC 78 ms
75,996 KB
testcase_21 AC 75 ms
71,216 KB
testcase_22 AC 74 ms
71,332 KB
testcase_23 AC 78 ms
76,040 KB
testcase_24 AC 78 ms
75,884 KB
testcase_25 AC 75 ms
71,400 KB
testcase_26 AC 77 ms
71,436 KB
testcase_27 AC 162 ms
76,748 KB
testcase_28 AC 291 ms
76,980 KB
testcase_29 AC 91 ms
76,760 KB
testcase_30 AC 134 ms
76,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Sieve_of_Eratosthenes(N,mode=False):
    """Nまでのエラトステネスの篩を実行

    N:自然数
    mode:False->素数のリスト,True->素数かどうかのリスト
    (False->[2,3,5,...],True->[False,False,True,True,False,True,...])
    """

    if N==0:
        return [None]

    T=[True]*(N+1)
    T[0]=None
    T[1]=False

    x=4
    while x<=N:
        T[x]=False
        x+=2

    x=9
    while x<=N:
        T[x]=False
        x+=6

    a=5
    Flag=1
    while a*a<=N:
        if T[a]:
            b=a*a
            c=2*a
            while b<=N:
                T[b]=False
                b+=c
        a+=2 if Flag else 4
        Flag^=1

    if mode:
        return T
    else:
        return [k for k in range(N+1) if T[k]]
#================================================
N=int(input())
S=Sieve_of_Eratosthenes(N)
X=[-float("inf")]*(N+1)
X[0]=0

for a in S:
    for k in range(N,a-1,-1):
        X[k]=max(X[k],X[k-a]+1)

print(X[N] if X[N]>-float("inf") else -1)
0