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 78 79 80 81 82 83 84 85 86 87 88 89
| #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double PI = acos(-1); const int N = 5e6 + 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], ans[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; } } } } void read(int &len, Complex *x) { char c; while (~scanf("%c", &c) && (c > '9' || c < '0')) ; do x[len++].x = c - '0'; while (~scanf("%c", &c) && (c <= '9' && c >= '0')); for (int i = 0, j = len - 1; i < j; i++, j--) swap(x[i], x[j]); } int main() { read(n, A); read(m, B); while ((1 << bit) < n + m + 1) bit++; tot = 1 << bit; for (int i = 0; i < tot; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1); 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; i++) ans[i] = A[i].x / tot + 0.5; for (int i = 0; i < n + m; i++) { ans[i + 1] += ans[i] / 10; ans[i] %= 10; } bool flag = false; for (int i = n + m - 1; i >= 0; i--) { flag |= ans[i]; if (flag) printf("%d", ans[i]); } return 0; }
|