結果

問題 No.206 数の積集合を求めるクエリ
ユーザー syak18
提出日時 2019-09-13 15:09:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 289 ms / 7,000 ms
コード長 2,289 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 177,660 KB
実行使用メモリ 23,636 KB
最終ジャッジ日時 2024-07-04 03:49:55
合計ジャッジ時間 6,850 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<complex<double>> fft(vector<complex<double>> a, bool inverse = 0) {
    int n = a.size();
    int h = (int)log2(n);

    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) swap(a[i], a[j]);
    }

    for (int b = 1; b < n; b *= 2) {
        for (int j = 0; j < b; j++) {
            complex<double> w = polar(1.0, (2 * M_PI) / (2 * b) * j * (inverse ? 1 : -1));
            for (int k = 0; k < n; k += b * 2) {
                complex<double> s = a[j + k];
                complex<double> t = a[j + k + b] * w;
                a[j + k] = s + t;
                a[j + k + b] = s - t;
            }
        }
    }
    if (inverse)
        for (int i = 0; i < n; i++) a[i] /= n;
    return a;
}
vector<complex<double>> ifft(vector<complex<double>> &a) { return fft(a, 1); }

vector<complex<double>> fft(vector<double> a, bool inverse = 0) {
    vector<complex<double>> a_complex(a.size());
    for (int i = 0; i < a.size(); i++) a_complex[i] = complex<double>(a[i], 0);
    return fft(a_complex, inverse);
}
vector<complex<double>> ifft(vector<double> a) {
    vector<complex<double>> a_complex(a.size());
    for (int i = 0; i < a.size(); i++) a_complex[i] = complex<double>(a[i], 0);
    return fft(a_complex, 1);
}

// 畳み込み : return v | v[k] = sum_{0<=j<=k}(a[j]b[k-j])
vector<double> convolve(vector<double> a, vector<double> b) {
    int s = a.size() + b.size() - 1;
    int t = 1;
    while (t < s) t *= 2;
    a.resize(t);
    b.resize(t);
    vector<complex<double>> A = fft(a);
    vector<complex<double>> B = fft(b);
    for (int i = 0; i < t; i++) {
        A[i] *= B[i];
    }
    A = ifft(A);
    for (int i = 0; i < A.size(); i++) a[i] = A[i].real();
    return a;
}

int main() {
    int l, m, n, q;
    cin >> l >> m >> n;
    vector<double> A(n + 1), B(n + 1);
    for (int i = 0; i < l; i++) {
        int x;
        cin >> x;
        A[x]++;
    }
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        B[n - x]++;
    }
    cin >> q;
    vector<double> ans = convolve(A, B);
    for (int i = 0; i < q; i++) {
        cout << (int)(ans[i + n] + 0.5) << endl;
    }
    return 0;
}
0