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 70 71 72 73 74 75 76 77
| #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double PI = acos(-1); const int N = 3e6 + 10; struct Complex { double x, y; Complex() {} Complex(double _x, double _y) { x = _x; y = _y; } Complex operator+(Complex k) { return Complex(x + k.x, y + k.y); } Complex operator-(Complex k) { return Complex(x - k.x, y - k.y); } Complex operator*(Complex k) { return Complex(x * k.x - y * k.y, x * k.y + y * k.x); } } A[N], B[N]; int n, m, bit, tot, rev[N]; void fft(Complex *x, int op) { for (int i = 0; i < tot; i++) if (rev[i] < i) swap(x[rev[i]], x[i]); for (int mid = 1; mid < tot; mid <<= 1) { Complex w1 = Complex(cos(PI / mid), sin(op * PI / mid)); for (int i = 0; i < tot; i += (mid << 1)) { Complex cur = Complex(1, 0); for (int j = 0; j < mid; j++, cur = cur * w1) { Complex p = x[i + j], q = cur * x[i + j + mid]; x[i + j] = p + q, x[i + j + mid] = p - q; } } } if (op == -1) for (int i = 0; i < tot; i++) x[i].x = x[i].x / tot + 0.5; } void init() { while ((1 << bit) < n + m - 1) bit++; tot = 1 << bit; for (int i = 1; i < tot; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1); } int main() { scanf("%d%d", &n, &m); n++, m++; for (int i = 0; i < n; i++) scanf("%lf", &A[i].x); for (int i = 0; i < m; i++) scanf("%lf", &B[i].x); init(); fft(A, 1); fft(B, 1); for (int i = 0; i < tot; i++) A[i] = A[i] * B[i]; fft(A, -1); for (int i = 0; i < n + m - 1; i++) printf("%d ", (int)A[i].x); return 0; }
|