結果
問題 |
No.206 数の積集合を求めるクエリ
|
ユーザー |
|
提出日時 | 2021-02-09 21:21:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 235 ms / 7,000 ms |
コード長 | 3,394 bytes |
コンパイル時間 | 1,321 ms |
コンパイル使用メモリ | 138,220 KB |
最終ジャッジ日時 | 2025-01-18 16:45:28 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#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; }