結果

問題 No.206 数の積集合を求めるクエリ
ユーザー outlineoutline
提出日時 2021-02-09 21:21:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 7,000 ms
コード長 3,394 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 140,560 KB
実行使用メモリ 28,072 KB
最終ジャッジ日時 2023-09-21 09:07:36
合計ジャッジ時間 6,458 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 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 8 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 8 ms
4,384 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 227 ms
27,836 KB
testcase_18 AC 219 ms
28,072 KB
testcase_19 AC 224 ms
27,984 KB
testcase_20 AC 215 ms
28,060 KB
testcase_21 AC 220 ms
28,004 KB
testcase_22 AC 217 ms
27,932 KB
testcase_23 AC 224 ms
28,008 KB
testcase_24 AC 232 ms
27,840 KB
testcase_25 AC 228 ms
27,876 KB
testcase_26 AC 223 ms
27,944 KB
testcase_27 AC 216 ms
27,920 KB
testcase_28 AC 224 ms
28,000 KB
testcase_29 AC 224 ms
28,068 KB
testcase_30 AC 220 ms
27,960 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);
struct Complex {
    double real, imag;
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}
    inline Complex& operator=(const Complex& c){
        real = c.real; imag = c.imag;
        return *this;
    }
};
inline Complex operator+(const Complex& x, const Complex& y){
    return {x.real + y.real, x.imag + y.imag};
}
inline Complex operator-(const Complex& x, const Complex& y){
    return {x.real - y.real, x.imag - y.real};
}
inline Complex operator*(const Complex& x, const Complex& y){
    return {x.real * y.real - x.imag * y.imag, x.real * y.imag + x.imag * y.real};
}
inline Complex operator*(const Complex& x, double a){
    return {x.real * a, x.imag * a};
}
inline Complex operator/(const Complex& x, double a){
    return {x.real / a, x.imag / a};
}

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 = x * zeta;
    }
    for(int i = m; i < n; ++i){
        a[i] = f[i - m] + x * g[i - m];
        x = 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] = 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