結果

問題 No.385 カップ麺生活
ユーザー xuzijian629xuzijian629
提出日時 2018-11-04 19:11:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,844 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 204,536 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 07:39:25
合計ジャッジ時間 3,767 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 7 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 9 ms
6,940 KB
testcase_07 AC 10 ms
6,944 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 24 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 12 ms
6,944 KB
testcase_14 AC 23 ms
6,940 KB
testcase_15 AC 15 ms
6,944 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 16 ms
6,944 KB
testcase_18 AC 6 ms
6,940 KB
testcase_19 AC 6 ms
6,940 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 22 ms
6,940 KB
testcase_22 AC 12 ms
6,944 KB
testcase_23 AC 23 ms
6,940 KB
testcase_24 AC 18 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 19 ms
6,940 KB
testcase_27 AC 21 ms
6,940 KB
testcase_28 AC 21 ms
6,944 KB
testcase_29 AC 8 ms
6,940 KB
testcase_30 AC 20 ms
6,944 KB
testcase_31 AC 4 ms
6,940 KB
testcase_32 AC 7 ms
6,944 KB
testcase_33 AC 13 ms
6,944 KB
testcase_34 AC 13 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
struct MillerRabin {    
    i64 modpow(i64 a, i64 n, i64 mod) {
        if (n == 0) return 1;
        if (n % 2 == 0) {
            i64 t = modpow(a, n / 2, mod);
            return t * t % mod;
        }
        return a * modpow(a, n - 1, mod) % mod;
    }

    i64 modinv(i64 n, i64 mod) {
        return modpow(n, mod - 2, mod);
    }

    bool is_prime(i64 n, int k = 50) {
        if (n == 2) return true;
        if (n < 2 || n % 2 == 0) return false;
        i64 d = n - 1;
        while (d % 2 == 0) {
            d /= 2;
        }
        for (int i = 0; i < k; i++) {
            i64 a = rnd() % (n - 2) + 1;
            i64 t = d;
            i64 y = modpow(a, t, n);
            while (t != n - 1 && y != 1 && y != n - 1) {
                y = modpow(y, 2, n);
                t *= 2;
            }
            if (y != n - 1 && t % 2 == 0) {
                return false;
            }
        }
        return true;
    }
};

int main() {
    int m, n;
    cin >> m >> n;
    vi as(n);
    for (int i = 0; i < n; i++) {
        cin >> as[i];
    }

    vi dp(m + 1, -1);
    // dp[i]: ちょうどi円で買えるカップ麺の個数の最大値
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m + 1; j++) {
            if (j - as[i] >= 0 && dp[j - as[i]] != -1) {
                dp[j] = max(dp[j], dp[j - as[i]] + 1);
            }
        }
    }

    MillerRabin p;
    i64 ans = 0;
    for (int i = 0; i <= m; i++) {
        ans = max(ans, dp[i]);
    }
    for (int i = 0; i <= m; i++) {
        if (p.is_prime(m - i) && dp[i] != -1) {
            ans += dp[i];
        }
    }
    cout << ans << endl;
}   
0