結果

問題 No.458 異なる素数の和
ユーザー rpy3cpprpy3cpp
提出日時 2017-03-26 14:05:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 861 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 13,208 KB
最終ジャッジ日時 2023-09-20 11:00:02
合計ジャッジ時間 9,122 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
12,452 KB
testcase_01 AC 1,849 ms
8,652 KB
testcase_02 TLE -
testcase_03 AC 410 ms
7,912 KB
testcase_04 AC 530 ms
8,496 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def primes2(limit):
    ''' returns a list of prime numbers upto limit.
    source: Rossetta code: Sieve of Eratosthenes
    http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Odds-only_version_of_the_array_sieve_above
    '''
    if limit < 2: return []
    if limit < 3: return [2]
    lmtbf = (limit - 3) // 2
    buf = [True] * (lmtbf + 1)
    for i in range((int(limit ** 0.5) - 3) // 2 + 1):
        if buf[i]:
            p = i + i + 3
            s = p * (i + 1) + i
            buf[s::p] = [False] * ((lmtbf - s) // p + 1)
    return [2] + [i + i + 3 for i, v in enumerate(buf) if v]

def solve(N):
    primes = primes2(N)
    dp = [-1] * (N + 1)
    dp[0] = 0
    for p in primes:
        for i in range(N, p - 1, -1):
            if dp[i - p] != -1:
                dp[i] = max(dp[i], dp[i - p] + 1)
    return dp[N]

N = int(input())
print(solve(N))
0