BZOJ2194: 快速傅立叶之二
给A,B,求C,$C_k=\sum_{i=0}^{n-1}A_iB_{i-k}$。
B反过来就是模板了。能不能算一道题呢?
1 #include<cstdio> 2 #include<cstdlib> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstring> 6 #include<complex> 7 #include<math.h> 8 //#include<iostream> 9 using namespace std; 10 11 int n,m,wei; 12 #define maxn 262222 13 const double pi=acos(-1); 14 typedef complex<double> cp; 15 cp a[maxn],b[maxn],c[maxn]; int rev[maxn]; 16 17 void dft(cp *a,int n,int type) 18 { 19 for (int i=0;i<n;i++) if (i<rev[i]) {cp t=a[i]; a[i]=a[rev[i]]; a[rev[i]]=t;} 20 for (int i=1;i<n;i<<=1) 21 { 22 cp base=cp(cos(pi/i),type*sin(pi/i)); 23 for (int j=0,p=i<<1;j<n;j+=p) 24 { 25 cp t=cp(1,0); 26 for (int k=0;k<i;k++,t=t*base) 27 { 28 cp tmp=t*a[j+k+i]; 29 a[j+k+i]=a[j+k]-tmp; 30 a[j+k]+=tmp; 31 } 32 } 33 } 34 } 35 36 void mul(cp *a,cp *b,cp *c) 37 { 38 for (int i=0;i<n;i++) 39 { 40 rev[i]=0; 41 for (int j=0;j<wei;j++) rev[i]|=((i>>j)&1)<<(wei-j-1); 42 } 43 dft(a,n,1); dft(b,n,1); for (int i=0;i<n;i++) c[i]=a[i]*b[i]; 44 dft(c,n,-1); for (int i=0;i<n;i++) c[i]/=n; 45 } 46 47 int main() 48 { 49 scanf("%d",&n); n--; 50 for (int i=0,x,y;i<=n;i++) scanf("%d",&x),a[i]=x,scanf("%d",&y),b[n-i]=y; 51 m=n+n; for (n=1,wei=0;n<=m;n<<=1,wei++); mul(a,b,c); 52 for (int i=(m>>1);i<=m;i++) printf("%d\n",(int)(c[i].real()+0.5)); 53 return 0; 54 }