結果

問題 No.1782 ManyCoins
ユーザー Pachicobue
提出日時 2021-12-11 16:43:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,820 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 196,876 KB
最終ジャッジ日時 2025-01-26 07:54:18
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
constexpr int NMAX = 20;
constexpr int WMAX = 1000000;
struct Deq
{
    Deq() {}
    int front() const
    {
        return BUF[head];
    }
    bool isEmpty() const
    {
        return (head == tail);
    }
    void pushFront(int v)
    {
        --head;
        if (head < 0) { head += (WMAX + 1); }
        BUF[head] = v;
    }
    void pushBack(int v)
    {
        BUF[tail++] = v;
        if (tail == WMAX + 1) { tail -= (WMAX + 1); }
    }
    void popFront()
    {
        head++;
        if (head == WMAX + 1) { head -= (WMAX + 1); }
    }
    int BUF[WMAX + 1];
    int head = 0, tail = 0;
};
constexpr int INF = 1 << 30;
int dp[WMAX];
int Ws[NMAX];
int main()
{
    using i64 = long long;
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int N;
    i64 L;
    std::cin >> N >> L;
    for (int i = 0; i < N; i++) {
        std::cin >> Ws[i];
    }
    std::sort(Ws, Ws + N, std::greater<int>{});
    const int W = Ws[0];
    std::fill(dp, dp + WMAX, INF);
    dp[0] = 0;
    Deq Q;
    Q.pushFront(0);
    while (not Q.isEmpty()) {
        const int w = Q.front();
        Q.popFront();
        for (int i = 1; i < N; i++) {
            int nw = w + Ws[i];
            if (nw >= W) {
                nw -= W;
                if (dp[nw] > dp[w] + 1) {
                    dp[nw] = dp[w] + 1;
                    Q.pushBack(nw);
                }
            } else {
                if (dp[nw] > dp[w]) {
                    dp[nw] = dp[w];
                    Q.pushFront(nw);
                }
            }
        }
    }
    i64 ans = 0;
    for (int w = 0; w < W; w++) {
        if (w > L) { break; }
        const i64 l = (L - w) / W;
        if (l < dp[w]) { continue; }
        ans += l - dp[w] + 1;
    }
    std::cout << ans - 1 << "\n";
    return 0;
}
0