結果

問題 No.206 数の積集合を求めるクエリ
ユーザー pekempeypekempey
提出日時 2016-10-30 19:14:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 197 ms / 7,000 ms
コード長 2,512 bytes
コンパイル時間 1,331 ms
コンパイル使用メモリ 164,016 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-03 19:56:03
合計ジャッジ時間 8,976 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 180 ms
6,816 KB
testcase_01 AC 178 ms
6,816 KB
testcase_02 AC 175 ms
6,944 KB
testcase_03 AC 177 ms
6,944 KB
testcase_04 AC 187 ms
6,940 KB
testcase_05 AC 184 ms
6,944 KB
testcase_06 AC 193 ms
6,944 KB
testcase_07 AC 182 ms
6,944 KB
testcase_08 AC 179 ms
6,940 KB
testcase_09 AC 180 ms
6,944 KB
testcase_10 AC 183 ms
6,940 KB
testcase_11 AC 187 ms
6,940 KB
testcase_12 AC 183 ms
6,940 KB
testcase_13 AC 183 ms
6,940 KB
testcase_14 AC 184 ms
6,944 KB
testcase_15 AC 185 ms
6,940 KB
testcase_16 AC 189 ms
6,940 KB
testcase_17 AC 189 ms
6,940 KB
testcase_18 AC 184 ms
6,940 KB
testcase_19 AC 191 ms
6,940 KB
testcase_20 AC 186 ms
6,940 KB
testcase_21 AC 183 ms
6,940 KB
testcase_22 AC 181 ms
6,940 KB
testcase_23 AC 181 ms
6,944 KB
testcase_24 AC 192 ms
6,944 KB
testcase_25 AC 197 ms
6,944 KB
testcase_26 AC 188 ms
6,944 KB
testcase_27 AC 186 ms
6,944 KB
testcase_28 AC 197 ms
6,940 KB
testcase_29 AC 191 ms
6,940 KB
testcase_30 AC 189 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define getchar getchar_unlocked

struct NumberTheoreticTransform {
    int mod;
    int root;

    NumberTheoreticTransform(int mod, int root) : mod(mod), root(root) {}

    int mul(int x, int y) {
        return int64_t(x) * y % mod;
    }

    int add(int x, int y) {
        return (x += y) >= mod ? x - mod : x;
    }

    int pow(int x, int y) {
        int res = 1;
        while (y > 0) {
            if (y & 1) res = mul(res, x);
            x = mul(x, x);
            y >>= 1;
        }
        return res;
    }

    int inv(int x) {
        return pow(x, mod - 2);
    }

    void ntt(std::vector<int> &a, bool rev = false) {
        int n = a.size();
        int h = 0;
        for (int i = 0; 1 << i < n; i++) h++;
        for (int i = 0; i < n; i++) {
            int j = 0;
            for (int k = 0; k < h; k++) j |= (i >> k & 1) << (h - 1 - k);
            if (i < j) std::swap(a[i], a[j]);
        }
        for (int i = 1; i < n; i *= 2) {
            int w = pow(root, (mod - 1) / (i * 2));
            if (rev) w = inv(w);
            for (int j = 0; j < n; j += i * 2) {
                int wn = 1;
                for (int k = 0; k < i; k++) {
                    int s = a[j + k + 0];
                    int t = mul(a[j + k + i], wn);
                    a[j + k + 0] = add(s, t);
                    a[j + k + i] = add(s, mod - t);
                    wn = mul(wn, w);
                }
            }
        }
        int v = inv(n);
        if (rev) for (int i = 0; i < n; i++) a[i] = mul(a[i], v);
    }

    std::vector<int> mul(std::vector<int> a, std::vector<int> b) {
        int s = a.size() + b.size() - 1;
        int t = 1;
        while (t < s) t *= 2;
        a.resize(t);
        b.resize(t);
        ntt(a);
        ntt(b);
        for (int i = 0; i < t; i++) {
            a[i] = mul(a[i], b[i]);
        }
        ntt(a, true);
        a.resize(s);
        return a;
    }
};

int in() {
    char c;
    while ((c = getchar()) < '0') if (c == EOF) return -1;
    int n = c - '0';
    while ((c = getchar()) >= '0') n = n * 10 + (c - '0');
    return n;
}

int main() {
    int L = in(), M = in(), N = in();
    const int X = 1 << 17;
    vector<int> a(X), b(X);
    for (int i = 0; i < L; i++) a[in()] = 1;
    for (int i = 0; i < M; i++) b[N - in()] = 1;
    NumberTheoreticTransform ntt(1012924417, 5);
    a = ntt.mul(a, b);
    int Q = in();
    for (int i = 0; i < Q; i++) {
        printf("%d\n", a[N + i]);
    }
}
0