結果

問題 No.206 数の積集合を求めるクエリ
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-31 03:29:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 416 ms / 7,000 ms
コード長 2,198 bytes
コンパイル時間 1,184 ms
コンパイル使用メモリ 116,544 KB
実行使用メモリ 43,648 KB
最終ジャッジ日時 2023-08-17 07:11:33
合計ジャッジ時間 7,607 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 7 ms
4,524 KB
testcase_07 AC 7 ms
4,608 KB
testcase_08 AC 8 ms
4,604 KB
testcase_09 AC 7 ms
4,420 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 12 ms
4,604 KB
testcase_13 AC 11 ms
4,584 KB
testcase_14 AC 12 ms
4,520 KB
testcase_15 AC 11 ms
4,420 KB
testcase_16 AC 11 ms
4,608 KB
testcase_17 AC 303 ms
43,480 KB
testcase_18 AC 307 ms
43,316 KB
testcase_19 AC 281 ms
43,500 KB
testcase_20 AC 252 ms
43,500 KB
testcase_21 AC 268 ms
43,500 KB
testcase_22 AC 260 ms
43,440 KB
testcase_23 AC 293 ms
43,464 KB
testcase_24 AC 416 ms
43,364 KB
testcase_25 AC 415 ms
43,368 KB
testcase_26 AC 394 ms
43,512 KB
testcase_27 AC 351 ms
43,648 KB
testcase_28 AC 394 ms
43,320 KB
testcase_29 AC 405 ms
43,500 KB
testcase_30 AC 379 ms
43,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

//https://atcoder.jp/contests/atc001/tasks/fft_c

template <typename T>
vector<complex<T>> fft(vector<complex<T>> a, bool inv = 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) swap(a[i], a[j]);
    }
    
    for (int b=1; b<N; b*=2){
        for (int j=0; j<b; j++){
            complex<T> w = polar(1.0, (2.0*M_PI) / (2.0*b) * j * (inv ? 1 : -1));
            for (int k=0; k<N; k += b*2){
                complex<T> s = a[j+k];
                complex<T> t = a[j+k+b] * w;
                a[j+k] = s + t;
                a[j+k+b] = s - t;
            }
        }
    }
    
    if (inv){
        for (int i=0; i<N; i++) a[i] /= N;
    }
    return a;
}

template <typename T>
vector<complex<T>> fft(vector<T> a, bool inv = false){
    vector<complex<T>> a_complex(a.size());
    for (int i=0; i<a.size(); i++) a_complex[i] = complex<T>(a[i], 0);
    return fft(a_complex, inv);
}

//convolution c[k] = sum(i=0, k) a[i]b[k-j]
template <typename T>
vector<T> convolution(vector<T> a, vector<T> b){
    int s = a.size() + b.size() - 1;
    int t = 1;
    while(t < s) t *= 2;
    a.resize(t); b.resize(t);
    vector<complex<T>> A = fft(a); //DFT
    vector<complex<T>> B = fft(b); //DFT

    for (int i=0; i<t; i++) A[i] *= B[i];
    A = fft(A, true); //IDFT
    a.resize(s);
    for (int i=0; i<s; i++) a[i] = A[i].real();
    
    return a;
}

int main(){

    int L, M, N, A, B, Q;
    cin >> L >> M >> N;
    vector<long double> a(N+1), b(N+1);
    for (int i=0; i<L; i++){
        cin >> A;
        a[A] = 1;
    }
    for (int i=0; i<M; i++){
        cin >> B;
        b[B] = 1;
    }
    reverse(b.begin(), b.end());
    vector<long double> c = convolution(a, b);
    
    cin >> Q;
    for (int i=0; i<Q; i++){
        cout << (int) (c[N+i] + 0.5) << endl;
    }

    return 0;
}
0