結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
76,276 KB
testcase_01 AC 171 ms
76,832 KB
testcase_02 AC 202 ms
76,900 KB
testcase_03 AC 107 ms
76,824 KB
testcase_04 AC 113 ms
76,732 KB
testcase_05 AC 329 ms
77,056 KB
testcase_06 AC 190 ms
76,764 KB
testcase_07 AC 91 ms
76,616 KB
testcase_08 AC 330 ms
76,976 KB
testcase_09 AC 95 ms
76,448 KB
testcase_10 AC 76 ms
71,384 KB
testcase_11 AC 377 ms
77,304 KB
testcase_12 AC 77 ms
71,768 KB
testcase_13 AC 76 ms
71,900 KB
testcase_14 AC 77 ms
71,768 KB
testcase_15 AC 76 ms
71,856 KB
testcase_16 AC 102 ms
76,700 KB
testcase_17 AC 78 ms
71,792 KB
testcase_18 AC 76 ms
71,728 KB
testcase_19 AC 76 ms
71,572 KB
testcase_20 AC 83 ms
76,316 KB
testcase_21 AC 77 ms
71,720 KB
testcase_22 AC 78 ms
71,800 KB
testcase_23 AC 81 ms
76,012 KB
testcase_24 AC 81 ms
76,180 KB
testcase_25 AC 78 ms
71,804 KB
testcase_26 AC 79 ms
71,508 KB
testcase_27 AC 184 ms
76,580 KB
testcase_28 AC 365 ms
77,284 KB
testcase_29 AC 95 ms
76,692 KB
testcase_30 AC 149 ms
76,976 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 = [-1]*(N+1)
dp[0] = (0,0)
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] == -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
    else:
        dp[i] = (count,prime)

ans = dp[-1]
if ans == -1:
    print(-1)
else:
    print(ans[0])
0