Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
yukicoder No.206
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <complex>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
using namespace std;
typedef long long ll;
typedef complex<double> cmpd;
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
rep(i, a){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
rep(i, a){
int input;
cin >> input;
p -> push_back(input);
}
}
template <typename T>
void output(T a, int precision) {
if(precision > 0){
cout << setprecision(precision) << a << "\n";
}
else{
cout << a << "\n";
}
}
// 高速フーリエ変換
vector<cmpd> dft(vector<cmpd> & f, int deg, bool dir){
// 終了条件
if (deg == 1) {
return f;
}
// 半分の長さのf0, f1をつくる
vector<cmpd> f0(deg >> 1), f1(deg >> 1);
// f0: 奇数番目, f1: 偶数番目
rep(i, deg >> 1){
f0[i] = f[i << 1];
f1[i] = f[i << 1 | 1];
}
// それぞれについてdft
f0 = dft(f0 , deg >> 1, dir);
f1 = dft(f1 , deg >> 1, dir);
// dir: 順変換ならtrue, 逆変換ならfalse
cmpd zeta = (dir == true) ? polar<double>(1, 2 * M_PI / deg) : polar<double>(1, -2 * M_PI / deg);
cmpd pow_zeta(1);
rep(i, deg){
f[i] = f0[i % (deg >> 1)] + pow_zeta * f1[i % (deg >> 1)];
pow_zeta *= zeta;
}
return f;
}
vector<cmpd> idft(vector<cmpd> & f, int deg){
// fのサイズは整形されている
vector<cmpd> ret = dft(f, deg, false);
rep(i, deg){
ret[i] /= deg;
}
return ret;
}
// 多項式乗算
vector<cmpd> multipoly (vector<cmpd> & a, vector<cmpd> & b){
// 多項式を2^n次に整形.係数が0のところは0詰め
int nmax = (int)a.size() + (int)b.size() - 1;
int n = 1;
while(n < nmax) n <<= 1;
a.resize(n);
b.resize(n);
vector<cmpd> fc(n);
vector<cmpd> fa = dft(a, n, true);
vector<cmpd> fb = dft(b, n, true);
rep(i, n){
fc[i] = fa[i] * fb[i];
}
return idft(fc, n);
}
int main(int argc, const char * argv[]) {
// source code
int L = inputValue();
int M = inputValue();
int N = inputValue();
N++;
vector<cmpd> A(N);
rep(i, L){
A[inputValue()] = 1;
}
vector<cmpd> B(N);
rep(i, M){
B[N - inputValue()] = 1;
}
int Q = inputValue();
auto C = multipoly(A, B);
repd(i, N, N + Q){
output((int)(C[i].real() + 0.5), 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment