| 1 | #include "../code/mathematics/intx.cpp" |
| 2 | |
| 3 | intx fastmul(intx an, intx bn) |
| 4 | { |
| 5 | stringstream ss; |
| 6 | ss << an; |
| 7 | string as = ss.str(); |
| 8 | |
| 9 | ss.str(""); |
| 10 | ss << bn; |
| 11 | string bs = ss.str(); |
| 12 | |
| 13 | int n = size(as), |
| 14 | m = size(bs), |
| 15 | len = 5, |
| 16 | radix = 100000; |
| 17 | |
| 18 | int *a = new int[n], |
| 19 | *b = new int[m]; |
| 20 | |
| 21 | int alen = 0, blen = 0; |
| 22 | for (int i = n - 1; i >= 0; i -= len) |
| 23 | { |
| 24 | int x = 0; |
| 25 | for (int j = len - 1; j >= 0; j--) |
| 26 | { |
| 27 | if (i - j < 0) continue; |
| 28 | x = x * 10 + as[i - j] - '0'; |
| 29 | } |
| 30 | |
| 31 | a[alen++] = x; |
| 32 | } |
| 33 | |
| 34 | for (int i = m - 1; i >= 0; i -= len) |
| 35 | { |
| 36 | int x = 0; |
| 37 | for (int j = len - 1; j >= 0; j--) |
| 38 | { |
| 39 | if (i - j < 0) continue; |
| 40 | x = x * 10 + bs[i - j] - '0'; |
| 41 | } |
| 42 | |
| 43 | b[blen++] = x; |
| 44 | } |
| 45 | |
| 46 | int l = 1; |
| 47 | while (l < alen || l < blen) l <<= 1; |
| 48 | l <<= 1; |
| 49 | |
| 50 | cpx *A = new cpx[l]; |
| 51 | cpx *B = new cpx[l]; |
| 52 | |
| 53 | for (int i = 0; i < l; i++) A[i] = cpx(i < alen ? a[i] : 0, 0); |
| 54 | for (int i = 0; i < l; i++) B[i] = cpx(i < blen ? b[i] : 0, 0); |
| 55 | |
| 56 | fft(A, l); |
| 57 | fft(B, l); |
| 58 | for (int i = 0; i < l; i++) A[i] *= B[i]; |
| 59 | fft(A, l, true); |
| 60 | |