結果

問題 No.458 異なる素数の和
ユーザー yassu0320yassu0320
提出日時 2021-07-03 16:16:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,551 ms / 2,000 ms
コード長 2,085 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 434,332 KB
最終ジャッジ日時 2023-09-12 20:53:16
合計ジャッジ時間 15,972 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
81,184 KB
testcase_01 AC 582 ms
171,108 KB
testcase_02 AC 678 ms
213,188 KB
testcase_03 AC 263 ms
103,640 KB
testcase_04 AC 284 ms
104,892 KB
testcase_05 AC 1,291 ms
372,116 KB
testcase_06 AC 787 ms
193,956 KB
testcase_07 AC 175 ms
81,652 KB
testcase_08 AC 1,253 ms
378,556 KB
testcase_09 AC 201 ms
82,168 KB
testcase_10 AC 161 ms
80,068 KB
testcase_11 AC 1,551 ms
434,332 KB
testcase_12 AC 193 ms
79,944 KB
testcase_13 AC 161 ms
79,920 KB
testcase_14 AC 163 ms
79,880 KB
testcase_15 AC 163 ms
79,852 KB
testcase_16 AC 215 ms
82,108 KB
testcase_17 AC 165 ms
80,128 KB
testcase_18 AC 164 ms
80,268 KB
testcase_19 AC 161 ms
79,780 KB
testcase_20 AC 162 ms
80,340 KB
testcase_21 AC 164 ms
79,916 KB
testcase_22 AC 162 ms
79,956 KB
testcase_23 AC 167 ms
79,904 KB
testcase_24 AC 166 ms
80,188 KB
testcase_25 AC 161 ms
79,844 KB
testcase_26 AC 165 ms
80,256 KB
testcase_27 AC 626 ms
193,832 KB
testcase_28 AC 1,422 ms
427,868 KB
testcase_29 AC 192 ms
82,664 KB
testcase_30 AC 445 ms
148,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

from sys import setrecursionlimit, stdin
from typing import Iterable

INF: int = 2**62
MOD: int = 10**9 + 7

setrecursionlimit(10**6)


def inputs(type_=int):
    ins = input().split(' ')
    ins = [x for x in ins if x != '']

    if isinstance(type_, Iterable):
        return [t(x) for t, x in zip(type_, ins)]
    else:
        return list(map(type_, ins))


def input_(type_=int):
    a, = inputs(type_)
    return a


inputi = input_


def inputstr():
    return input_(str)


# b/a縺ョ蛻・j荳翫£
def ceil(b, a):
    return (a + b - 1) // a


def answer(res) -> None:
    print(res)
    exit()


def compute():
    return

def prime_iter():
    """
    繧ィ繝ゥ繝医せ繝・ロ繧ケ縺ョ遽ゥ
    險育ョ鈴㍼縺ッ O(n * sqrt(n) / log(n))
    """
    ps = []
    n = 2
    while True:
        for k in ps:
            if n % k == 0:
                break
            elif k * k > n:
                ps.append(n)
                yield n
                break
        else:
            ps.append(n)
            yield n

        n += 1

def main():
    n = inputi()

    # ps[i]: i逡ェ逶ョ縺ォ蟆上&縺・エ謨ー (ps[i] <= max(n//2, p-n//2))
    # pi = len(ps)
    # dp[i][k]: ps[i]繧剃スソ縺」縺ヲk繧剃ス懊k譎ゅ・譛€螟ァ縺ョ蜥後・蝗樊焚
    #
    # dp[0][0] = 0
    # 蛻晄悄蛹・ dp = [[-1] * (n+1) for _ in range(ps)]
    # 譖エ譁ー:
    #     dp[i][k] = max(dp[i][k-1], dp[i-1][k-ps[i]] + 1)
    # 蜃コ蜉・ dp[-1][n]

    ps = []
    iter_ = prime_iter()
    while True:
        p = next(iter_)
        if p > n:
            break
        ps.append(p)

    ps = [0] + ps
    plen = len(ps)
    dp = [[-1] * (n+1) for _ in range(plen)]
    dp[0][0] = 0

    for i in range(1, plen):
        for k in range(n+1):
            dp[i][k] = dp[i-1][k]
            if k - ps[i] >= 0 and dp[i-1][k-ps[i]] >= 0:
                dp[i][k] = max(1 + dp[i-1][k-ps[i]], dp[i-1][k])

    # from pprint import pprint
    # pprint(dp)
    print(dp[-1][n])


if __name__ == '__main__':
    main()
0