結果

問題 No.458 異なる素数の和
ユーザー ayaoniayaoni
提出日時 2020-12-05 20:52:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 1,518 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 77,096 KB
最終ジャッジ日時 2023-10-14 08:13:24
合計ジャッジ時間 6,160 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
76,388 KB
testcase_01 AC 169 ms
76,740 KB
testcase_02 AC 197 ms
76,756 KB
testcase_03 AC 104 ms
76,716 KB
testcase_04 AC 112 ms
76,864 KB
testcase_05 AC 331 ms
77,084 KB
testcase_06 AC 188 ms
76,860 KB
testcase_07 AC 83 ms
76,644 KB
testcase_08 AC 329 ms
77,012 KB
testcase_09 AC 93 ms
76,516 KB
testcase_10 AC 73 ms
71,400 KB
testcase_11 AC 383 ms
76,828 KB
testcase_12 AC 73 ms
71,896 KB
testcase_13 AC 74 ms
71,588 KB
testcase_14 AC 72 ms
71,440 KB
testcase_15 AC 72 ms
71,676 KB
testcase_16 AC 95 ms
76,804 KB
testcase_17 AC 72 ms
71,680 KB
testcase_18 AC 71 ms
71,976 KB
testcase_19 AC 72 ms
71,632 KB
testcase_20 AC 77 ms
76,352 KB
testcase_21 AC 73 ms
71,848 KB
testcase_22 AC 74 ms
71,636 KB
testcase_23 AC 78 ms
76,376 KB
testcase_24 AC 77 ms
76,364 KB
testcase_25 AC 71 ms
71,740 KB
testcase_26 AC 73 ms
71,764 KB
testcase_27 AC 183 ms
76,612 KB
testcase_28 AC 370 ms
77,096 KB
testcase_29 AC 88 ms
76,520 KB
testcase_30 AC 146 ms
77,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


def sieve_of_eratosthenes(n):  # n以下の素数の全列挙
    prime_list = []
    A = [1]*(n+1)  # A[i] = iが素数なら1,その他は0
    A[0] = A[1] = 0
    for i in range(2,int(n**.5)+1):
        if A[i]:
            prime_list.append(i)
            for j in range(i**2,n+1,i):
                A[j] = 0
    for i in range(int(n**.5)+1,n+1):
        if A[i] == 1:
            prime_list.append(i)
    return prime_list


N = I()
primes = sieve_of_eratosthenes(N)

dp = [(0,0)]*(N+1)
# dp[i] = (iを構成する素数の最大個数,iを構成する最大の素数の最小値)
inf = 10**18
for i in range(1,N+1):
    count = 0
    prime = inf
    for p in primes:
        if p > i:
            break
        if dp[i-p][0] == -1:
            continue
        a,b = dp[i-p]
        if p <= b or a+1 < count:
            continue
        if a+1 == count:
            prime = min(prime,p)
        elif a+1 > count:
            count = a+1
            prime = p
    if count == 0:
        dp[i] = (-1,0)
    else:
        dp[i] = (count,prime)

print(dp[-1][0])
0