結果

問題 No.206 数の積集合を求めるクエリ
ユーザー face4face4
提出日時 2020-01-01 20:55:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 402 ms / 7,000 ms
コード長 2,218 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 90,680 KB
実行使用メモリ 27,680 KB
最終ジャッジ日時 2023-08-14 17:54:57
合計ジャッジ時間 6,763 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 9 ms
4,376 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 10 ms
4,376 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 273 ms
27,648 KB
testcase_18 AC 263 ms
27,656 KB
testcase_19 AC 264 ms
27,528 KB
testcase_20 AC 265 ms
27,544 KB
testcase_21 AC 271 ms
27,612 KB
testcase_22 AC 265 ms
27,528 KB
testcase_23 AC 260 ms
27,532 KB
testcase_24 AC 368 ms
27,568 KB
testcase_25 AC 375 ms
27,644 KB
testcase_26 AC 371 ms
27,608 KB
testcase_27 AC 338 ms
27,556 KB
testcase_28 AC 386 ms
27,648 KB
testcase_29 AC 402 ms
27,680 KB
testcase_30 AC 395 ms
27,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cmath>
#include<complex>
#include<vector>
using namespace std;

const double PI = acos(0.0)*2;

void fft(vector<complex<double>> &a, bool inverse=false){
    int n = a.size();
    int h = 0;
    while(1<<h < n) 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)   swap(a[i], a[j]);
    }
    // butter-fly
    for(int b = 1; b < n; b *= 2){  // ステップb、ブロックサイズ2*b
        for(int j = 0; j < b; j++){ // ブロック内j個目
            complex<double> w = polar(1.0, -(2*PI) / (2*b) * j * (inverse ? -1 : 1));
            for(int k = 0; k < n; k += 2*b){    // kを先頭とするブロック
                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;
}

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

void 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(t);
    vector<complex<double>> B(t);
    for(int i = 0; i < a.size(); i++)   A[i] = complex<double>(a[i], 0);
    for(int i = 0; i < b.size(); i++)   B[i] = complex<double>(b[i], 0);
    fft(A);
    fft(B);
    for(int i = 0; i < t; i++)  A[i] *= B[i];
    fft(A, true);
    a.resize(s);
    for(int i = 0; i < s; i++)  a[i] = A[i].real();
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int l, m, n;
    cin >> l >> m >> n;
    vector<double> a(2*n+1, 0), b(2*n+1, 0);
    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]++;
    }
    convolve(a, b);
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        cout << (int)(a[n+i]+0.5) << endl;
    }
    return 0;
}
0