Instantly share code, notes, and snippets.

# ctylim/yukicoder206.cpp Created Sep 13, 2015

What would you like to do?
yukicoder No.206
 #include #include #include #include #include #include #include #include #include #include #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 cmpd; int inputValue(){ int a; cin >> a; return a; }; void inputArray(int * p, int a){ rep(i, a){ cin >> p[i]; } }; void inputVector(vector * p, int a){ rep(i, a){ int input; cin >> input; p -> push_back(input); } } template void output(T a, int precision) { if(precision > 0){ cout << setprecision(precision) << a << "\n"; } else{ cout << a << "\n"; } } // 高速フーリエ変換 vector dft(vector & f, int deg, bool dir){ // 終了条件 if (deg == 1) { return f; } // 半分の長さのf0, f1をつくる vector 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(1, 2 * M_PI / deg) : polar(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 idft(vector & f, int deg){ // fのサイズは整形されている vector ret = dft(f, deg, false); rep(i, deg){ ret[i] /= deg; } return ret; } // 多項式乗算 vector multipoly (vector & a, vector & 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 fc(n); vector fa = dft(a, n, true); vector 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 A(N); rep(i, L){ A[inputValue()] = 1; } vector 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; }