結果

問題 No.206 数の積集合を求めるクエリ
ユーザー null_mnnull_mn
提出日時 2018-07-27 14:05:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 369 ms / 7,000 ms
コード長 2,052 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 169,308 KB
実行使用メモリ 21,520 KB
最終ジャッジ日時 2023-09-13 18:53:25
合計ジャッジ時間 9,587 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 245 ms
21,336 KB
testcase_18 AC 239 ms
21,332 KB
testcase_19 AC 246 ms
21,520 KB
testcase_20 AC 239 ms
21,508 KB
testcase_21 AC 243 ms
21,452 KB
testcase_22 AC 241 ms
21,512 KB
testcase_23 AC 247 ms
21,516 KB
testcase_24 AC 369 ms
21,384 KB
testcase_25 AC 368 ms
21,320 KB
testcase_26 AC 366 ms
21,400 KB
testcase_27 AC 328 ms
21,460 KB
testcase_28 AC 362 ms
21,508 KB
testcase_29 AC 367 ms
21,328 KB
testcase_30 AC 367 ms
21,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
#define REP(i,m,n) for(int i = m; i < (int)(n); ++i)
#define rep(i,n) REP(i, 0, n)
//------------------------------------------------------

class FFT { //f*gを高速に求める
public:
    using C = complex<double>;
    vector<C> f, g;
    int s; //max(deg(f), deg(g))

    int decideSize(int n) {
        int m = 1 << (32 - __builtin_clz(2 * n - 2));
        //m >= n+1, m = 2^a となる最小のm
        s = n;
        f.resize(m);
        g.resize(m);
        return m;
    }

    void dft(vector<C> &fp, double inv = 1.0) {
        int n = fp.size();
        if (n == 1) return;
        vector<C> f0(n / 2), f1(n / 2);
        for (int i = 0; i * 2 < n; ++i) { //fpをf0とf1に分割
            f0[i] = fp[2 * i];
            f1[i] = fp[2 * i + 1];
        }
        dft(f0, inv);
        dft(f1, inv);
        C pow_zeta = 1, zeta = polar(1.0, inv * 2 * PI / n); //1のn乗根
        int m = n / 2 - 1;

        rep(i, n) {
            fp[i] = f0[i & m] + pow_zeta * f1[i & m];
            pow_zeta *= zeta;
        }
    }

    void inv_dft(vector<C> &f_) {
        dft(f_, -1.0);
        C n = C(f_.size());
        for (auto&& i : f_) i /= n;
    }

    vector<int> ans() {
        vector<int> fg(2 * s - 1); //f*g
        dft(f);
        dft(g);
        int m = decideSize(s);
        rep(i, m) f[i] *= g[i];
        inv_dft(f);
        rep(i, 2 * s - 1) fg[i] = (int) round(f[i].real());
        return fg;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int l, m, n;
    cin >> l >> m >> n;
    FFT d;
    d.decideSize(n + 1);
    vector<int> va(n + 1, 0), vb(n + 1, 0);

    rep(i, l) {
        int a;
        cin >> a;
        va[a]++;
    }

    rep(i, m) {
        int b;
        cin >> b;
        vb[n - b]++;
    }

    rep(i, n + 1) {
        d.f[i] = va[i];
        d.g[i] = vb[i];
    }

    int q;
    cin >> q;
    vector<int> ans = d.ans();

    rep(i, q) {
        cout << ans[n + i] << endl;
    }
    return 0;
}
0