1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e6 + 10; const double pi = acos(-1.0); int n, m, r[N], lim = 1, l; struct com{ double x, y; com(double xx = 0, double yy = 0){ x = xx; y = yy; } }a[N], b[N];
com operator + (com a, com b) {return com(a.x + b.x, a.y + b.y);} com operator - (com a, com b) {return com(a.x - b.x, a.y - b.y);} com operator * (com a, com b) {return com(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
void fft(com *x, int type){
for(int i = 0; i < lim; ++ i){ if(r[i] > i) swap(x[i], x[r[i]]); }
for(int mid = 1; mid < lim; mid <<= 1){ com k(cos(pi / mid), type * sin(pi / mid)) ; for(int R = mid << 1, j = 0; j < lim; j += R){ com w(1, 0); for(int i = 0; i < mid; ++ i, w = w * k){ com aa = x[j + i], bb = w * x[j + i + mid];
x[i + j] = aa + bb; x[i + j + mid] = aa - bb; } } } } int main(){ cin >> n >> m; for(int i = 0; i <= n; ++ i) cin >> a[i].x; for(int i = 0; i <= m; ++ i) cin >> b[i].x; while(lim <= n + m) lim <<= 1, l ++;
for(int i = 0; i < lim; ++ i) r[i] = (r[i >> 1] >> 1 | (i & 1) << (l - 1));
fft(a, 1); fft(b, 1); for(int i = 0; i <= lim; ++ i) a[i] = a[i] * b[i];
fft(a, -1);
for(int i = 0; i <= n + m; ++ i) { int xx = a[i].x / lim + 0.5; cout << xx << ' '; } }
|