[BZOJ2179] FFT high-precision multiplication template, bzoj2179fft
Advertisement:
# Include <stdio. h> int main () {puts ("reprinted please specify the source [vmurder] Thank you"); puts ("url: blog.csdn.net/vmurder/article/details/42515679 ");}
Question:
In fact, there is no problem, just paste a template + understand the comment
Code:
# Include <cmath> # include <cstdio> # include <cstring> # include <complex> # include <iostream> # include <algorithm> using namespace std; # define N 131075int n, c [N]; complex <double> a [N], B [N], p [N]; const double pi = acos (-1 ); void FFT (complex <double> x [], int n, int type) {int I, j, k, t;/* flip by binary: for example, 01234567-> 04261537 000 000 001 100 010 011 110 100 001 101 101 011 110 111 */for (I = 0, t = 0; I <n; I ++) // flip by bit {If (I> t) swap (x [I], x [t]); for (j = n> 1; (t ^ = j) <j; j >>> = 1 );} // for wn // type = 1, the positive Rotation Unit angle on the secondary plane // type =-1, the negative Rotation Unit angle on the secondary plane for (k = 2; k <= n; k <= 1) {complex <double> wn (cos (type * 2 * pi/k ), sin (type * 2 * pi/k); for (I = 0; I <n; I + = k) {complex <double> w (1, 0), t; // you can directly write w = 1, x axis is real, and Y axis is virtual. For (j = 0; j <k> 1; w * = wn, j ++) t = w * x [I + j + (k> 1)], x [I + j + (k> 1)] = x [I + j]-t, x [I + j] + = t ;}} int main () {int I, j, k; cin> n; for (getchar (), I = n-1; I> = 0; I --) a [I] = getchar () -'0'; for (getchar (), I = n-1; I> = 0; I --) B [I] = getchar ()-'0 '; for (j = n, I = 1; I> 2 <j; I <= 1) n = I; FFT (a, n, 1), FFT (B, n, 1); // convert the past for (I = 0; I <n; I ++) p [I] = a [I] * B [I]; FFT (p, n,-1); // convert back int len = 0; // output for (I = 0; I <n; I ++) c [I] = p [I]. real ()/n + 0.1; // eps = 0.1 for (I = 0; I <n; I ++) if (c [I]) len = I, c [I + 1] + = c [I]/10, c [I] % = 10; for (I = len; I> = 0; I --) printf ("% d", c [I]); return 0 ;}