結果

問題 No.2581 [Cherry Anniversary 3] 28輪の桜のブーケ
ユーザー fdironiafdironia
提出日時 2023-12-09 14:28:32
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 3,000 ms
コード長 1,669 bytes
コンパイル時間 1,373 ms
コンパイル使用メモリ 99,764 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-09 14:28:36
合計ジャッジ時間 3,183 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

using vi = vector<int>;

constexpr int N = 28;

void prep(int g[], int h[], int n, int m, vi b[]) {
    for (int i = 0; i < 1 << n; i++) {
        int pc = 0, cb = 0;
        for (int j = 0; j < n; j++) {
            if (i >> j & 1) {
                pc++;
                cb = (cb + g[j]) % m;
            } else {
                cb = (cb + h[j]) % m;
            }
        }

        b[pc].push_back(cb);
    }

    for (int i = 0; i <= n; i++) {
        sort(b[i].begin(), b[i].end());
    }
}

int calc(vi &a, vi &b, int l, int r) {
    int ret = 0;
    for (int i = 0, j0 = (int)b.size() - 1, j1 = (int)b.size() - 1;
         i < (int)a.size(); i++) {
        while (j0 >= 0 && a[i] + b[j0] >= l) {
            j0--;
        }
        while (j1 >= 0 && a[i] + b[j1] >= r) {
            j1--;
        }

        ret += j1 - j0;
    }

    return ret;
}

int main() {
    int m;
    cin >> m;
    int g[N], h[N];
    for (int i = 0; i < N; i++) {
        cin >> g[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> h[i];
    }

    vi b0[N/2+1], b1[N/2+1];
    prep(g, h, N / 2, m, b0);
    prep(g + N / 2, h + N / 2, N / 2, m, b1);

    int q;
    cin >> q;

    while (q--) {
        int k, x;
        cin >> k >> x;

        int ans = 0;
        for (int u = 0; u <= N / 2; u++) {
            for (int v = 0; v <= N / 2; v++) {
                if (u + v == k) {
                    ans += calc(b0[u], b1[v], x, m) +
                           calc(b0[u], b1[v], x + m, 2 * m);
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}
0