Polynomial multiplication
1#include <bits/stdc++.h>2 using namespacestd;3 Const DoublePI = ACOs (-1);4 Const intLEN = 1e5 +5;5 intN, M, N;6 namespacePloy {7 structComp {8 Doublex, y;9} A[len *4], B[len *4];TenCompoperator+ (ConstComp &x,ConstComp &y) { One return(comp) {x.x + y.x, x.y +y.y}; A } -Compoperator- (ConstComp &x,ConstComp &y) { - return(comp) {x.x-y.x, x.y-y.y}; the } -Compoperator* (ConstComp &x,ConstComp &y) { - return(comp) {x.x * y.x-x.y * y.y, x.x * y.y + x.y *y.x}; - } + voidFFT (Comp *a,intNintx) { - for(inti = n >>1, j =1; J < N; J + +) { + if(I <j) Swap (A[i], a[j]); A intk = n >>1; at for(k & i; I ^= K, K >>=1); -I ^=K; - } - for(intm =2; M <= N; M <<=1) { -Comp W = (comp) {cos (2.0* pi * x/m), sin (2.0* pi * x/m)}; - for(inti =0; i + M <= N; i + =m) { inComp T = (comp) {1,0}; - for(intj = i; J < i + (M >>1); J + +) { tocomp U =A[j]; +Comp v = t * a[j + (M >>1)]; -A[J] = U +v; theA[j + (M >>1)] = U-v; *t = T *W; $ }Panax Notoginseng } - } the if(x = =-1) { + for(inti =0; I < n; i++) a[i].x/=N; A } the } + } - using namespaceploy; $ intMain () { $scanf"%d%d", &n, &m); -n++, m++; -N =1; the while(n < n + M-1) N <<=1; - for(inti =0; I < n; i++) scanf ("%LF", &a[i].x);Wuyi for(inti =0; I < m; i++) scanf ("%LF", &b[i].x); theFFT (A, N,1); -FFT (b, N,1); Wu for(inti =0; i < N; i++) A[i] = a[i] *B[i]; -FFT (A, N,-1); About for(inti =0; I < n + M-2; i++) printf ("%d", (int) (a[i].x +0.5)); $printf"%d\n", (int) (A[n + M-2].x +0.5)); - return 0; -}
FFT
Polynomial-related algorithm templates