結果

問題 No.206 数の積集合を求めるクエリ
ユーザー outlineoutline
提出日時 2021-02-09 20:19:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 7,000 ms
コード長 2,642 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 140,780 KB
実行使用メモリ 28,128 KB
最終ジャッジ日時 2023-09-21 08:01:36
合計ジャッジ時間 6,944 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,388 KB
testcase_06 AC 8 ms
4,384 KB
testcase_07 AC 8 ms
4,380 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 9 ms
4,384 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 255 ms
27,940 KB
testcase_18 AC 248 ms
27,872 KB
testcase_19 AC 253 ms
27,880 KB
testcase_20 AC 244 ms
28,008 KB
testcase_21 AC 246 ms
27,936 KB
testcase_22 AC 246 ms
27,948 KB
testcase_23 AC 252 ms
27,868 KB
testcase_24 AC 260 ms
27,872 KB
testcase_25 AC 257 ms
27,868 KB
testcase_26 AC 253 ms
28,128 KB
testcase_27 AC 248 ms
28,008 KB
testcase_28 AC 252 ms
27,952 KB
testcase_29 AC 254 ms
27,944 KB
testcase_30 AC 250 ms
27,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

const double pi = acos(-1);
using Complex = complex<double>;

vector<Complex> dft(vector<Complex>& a, int n, int sgn = 1){
    if(n == 1)  return a;

    int m = n / 2;
    vector<Complex> f(m), g(m);
    for(int i = 0; i < m; ++i){
        f[i] = a[2 * i];
        g[i] = a[2 * i + 1];
    }

    // divide and conquer
    f = dft(f, m, sgn);
    g = dft(g, m, sgn);

    // zeta : nth root of 1
    Complex zeta(cos(2.0 * pi / n), sin(2.0 * pi / n) * sgn);
    Complex x = 1;

    for(int i = 0; i < m; ++i){
        a[i] = f[i] + x * g[i];
        x *= zeta;
    }
    for(int i = m; i < n; ++i){
        a[i] = f[i - m] + x * g[i - m];
        x *= zeta;
    }

    return a;
}

vector<Complex> inv_dft(vector<Complex>& a, int n){
    a = dft(a, n, -1);
    for(int i = 0; i < n; ++i)  a[i] /= n;
    return a;
}

vector<int64_t> multiply(vector<Complex>& a, vector<Complex>& b){
    int sz = a.size() + b.size() - 1;
    int n = 1;
    while(n < sz)   n <<= 1;

    a.resize(n); b.resize(n);
    a = dft(a, n);
    b = dft(b, n);

    vector<Complex> f(n);
    for(int i = 0; i < n; ++i){
        f[i] = a[i] * b[i];
    }
    f = inv_dft(f, n);

    vector<int64_t> res(n);
    for(int i = 0; i < n; ++i){
        res[i] = llround(f[i].real());
    }

    return res;
}

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

    int L, M, N, A, B, Q;
    cin >> L >> M >> N;
    vector<Complex> a(N), b(N);
    for(int i = 0; i < L; ++i){
        cin >> A;
        a[A - 1] = Complex(1, 0);
    }
    for(int i = 0; i < M; ++i){
        cin >> B;
        b[N - B] = Complex(1, 0);
    }
    auto c = multiply(a, b);
    cin >> Q;
    for(int i = 0; i < Q; ++i){
        cout << c[N - 1 + i] << '\n';
    }

    return 0;
}
0