結果

問題 No.385 カップ麺生活
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-02 11:51:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-15 07:30:13
合計ジャッジ時間 2,889 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,880 KB
testcase_01 AC 32 ms
10,880 KB
testcase_02 AC 32 ms
10,880 KB
testcase_03 AC 33 ms
10,880 KB
testcase_04 AC 37 ms
10,880 KB
testcase_05 AC 33 ms
10,752 KB
testcase_06 AC 51 ms
10,752 KB
testcase_07 AC 47 ms
11,008 KB
testcase_08 AC 39 ms
10,880 KB
testcase_09 AC 31 ms
10,752 KB
testcase_10 AC 146 ms
11,008 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 44 ms
10,880 KB
testcase_13 AC 62 ms
11,008 KB
testcase_14 AC 68 ms
11,008 KB
testcase_15 AC 61 ms
11,008 KB
testcase_16 AC 34 ms
10,880 KB
testcase_17 AC 83 ms
11,008 KB
testcase_18 AC 47 ms
10,880 KB
testcase_19 AC 38 ms
10,880 KB
testcase_20 AC 61 ms
11,008 KB
testcase_21 AC 69 ms
11,008 KB
testcase_22 AC 63 ms
10,880 KB
testcase_23 AC 75 ms
10,880 KB
testcase_24 AC 67 ms
11,008 KB
testcase_25 AC 31 ms
10,752 KB
testcase_26 AC 41 ms
11,008 KB
testcase_27 AC 55 ms
11,008 KB
testcase_28 AC 60 ms
11,008 KB
testcase_29 AC 44 ms
11,008 KB
testcase_30 AC 69 ms
11,008 KB
testcase_31 AC 32 ms
10,880 KB
testcase_32 AC 39 ms
10,880 KB
testcase_33 AC 75 ms
11,008 KB
testcase_34 AC 43 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# Based on http://pakapa104.hatenablog.com/entry/2016/07/02/014219

import array


INF = 10 ** 9


def sieve_of_eratosthenes(end):
    is_prime = array.array("B", (True for i in range(end)))
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, end):
        if is_prime[i]:
            for j in range(2 * i, end, i):
                is_prime[j] = False
    return is_prime


def compute_max_cups(init_money, cups):
    # 個数制限のないナップザック問題をボトムアップの動的計画法で解く
    # dp[i] = (所持金がiになるまでに買えるカップ麺の数)
    dp = array.array("l", (-INF for _ in range(init_money + 1)))
    dp[init_money] = 0
    for cup in cups:
        for i in range(init_money - cup + 1)[::-1]:
            dp[i] = max(dp[i], dp[i + cup] + 1)
    # 素数表を得る
    is_prime = sieve_of_eratosthenes(init_money + 1)
    # 金欠チャンスの処理
    # その後は、買えるだけ買う
    return sum(dp[i] for i in range(1, init_money + 1)
               if dp[i] > 0 and is_prime[i]) + max(dp)


def main():
    m = int(input())
    _ = int(input())
    cs = array.array("L", map(int, input().split()))
    print(compute_max_cups(m, cs))


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