1 struct Complex{ 2 double x, y; 3 inline Complex(double xx=0, double yy=0){ 4 x=xx; y=yy; 5 } 6 inline Complex operator + (Complex a){ 7 return Complex(x+a.x, y+a.y); 8 } 9 inline Complex operator - (Complex a){ 10 return Complex(x-a.x, y-a.y); 11 } 12 inline Complex operator * (Complex a){ 13 return Complex(x*a.x-y*a.y, x*a.y+y*a.x); 14 } 15 }A[N], B[N]; 16 17 int n, m, L; 18 int rev[N]; 19 20 void FFT(Complex *a, int le, int ty){ 21 for (int i=0; i<le; ++i) 22 if (i < rev[i]) swap(a[i], a[rev[i]]); 23 for (int i=1; i<le; i<<=1){ 24 Complex wn=Complex(cos(pi/i), ty*sin(pi/i)); 25 for (int j=0; j<le; j+=i<<1){ 26 Complex w=Complex(1, 0), x, y; 27 for (int k=0; k<i; ++k, w=w*wn){ 28 x=a[j+k]; y=a[j+i+k]*w; 29 a[j+k]=x+y; a[j+i+k]=x-y; 30 } 31 } 32 } 33 } 34 35 int main(){ 36 scanf("%d%d", &n, &m); ++n; ++m; 37 for (L=1; L<n+m-1; L<<=1) continue; 38 for (int i=0; i<L; ++i){ 39 rev[i]=rev[i>>1]>>1; 40 if (i&1) rev[i]|=L>>1; 41 } 42 for (int i=0, z; i<n; ++i) scanf("%d", &z), A[i].x=z; 43 for (int i=0, z; i<m; ++i) scanf("%d", &z), B[i].x=z; 44 FFT(A, L, 1); 45 FFT(B, L, 1); 46 for (int i=0; i<L; ++i) A[i]=A[i]*B[i]; 47 FFT(A, L, -1); 48 for (int i=0; i<n+m-1; ++i) printf("%d ", (int)(A[i].x/L+0.5)); 49 50 return 0; 51 }
原文地址:https://www.cnblogs.com/Dance-Of-Faith/p/8461382.html
时间: 2024-11-06 07:43:56