快速傅里叶变换(FFT)递归

匿名 (未验证) 提交于 2019-12-02 23:49:02
#include <cstdio> #include <cmath> #define Maxn 5000 #define Pi 3.1415926535898 using namespace std; int n,m; struct complex {complex (double xx=0,double yy=0){x=xx,y=yy;} double x,y;}; complex operator + (complex a,complex b){   return complex(a.x+b.x,a.y+b.y); }complex operator - (complex a,complex b){   return complex(a.x-b.x,a.y-b.y); }complex operator * (complex a,complex b){   return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); }complex w[Maxn],b[Maxn],c[Maxn]; void fft(complex *f,int len,short op) {   if (!len)return ;   complex fl[len+1],fr[len+1];   for (int k=0;k<len;k++)    {fl[k]=f[k<<1];fr[k]=f[k<<1|1];}   fft(fl,len>>1,op);   fft(fr,len>>1,op);   complex tmp,buf;   tmp=complex(cos(Pi/len),sin(Pi/len)*op);   buf.x=1;buf.y=0;   for (int k=0;k<len;k++){     complex t=buf*fr[k];     f[k]=fl[k]+t;     f[k+len]=fl[k]-t;     buf=buf*tmp;   } } int main() {   scanf("%d%d",&n,&m);   for (int i=0;i<=n;i++)scanf("%lf",&b[i].x);   for (int i=0;i<=m;i++)scanf("%lf",&c[i].x);   for(m+=n,n=1;n<=m;n<<=1);   fft(b,n>>1,1);   fft(c,n>>1,1);   for(int i=0;i<n;++i)b[i]=b[i]*c[i];   fft(b,n>>1,-1);   for(int i=0;i<=m;++i)printf("%.0f ",fabs(b[i].x)/n);   puts("");   return 0; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!