码迷,mamicode.com
首页 > 其他好文 > 详细

[模板] 快速傅里叶变换/fft

时间:2019-01-16 21:40:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:傅里叶变换   operator   return   void   for   快速   a+b   swap   swa   

Code

const int nmax=(int)3e6+50;
const db pi=acos(-1.0);

struct tcpx{db a,b;}c1[nmax],c2[nmax];
tcpx operator+(tcpx a,tcpx b){return (tcpx){a.a+b.a,a.b+b.b};}
tcpx operator-(tcpx a,tcpx b){return (tcpx){a.a-b.a,a.b-b.b};}
tcpx operator*(tcpx a,tcpx b){return (tcpx){a.a*b.a-a.b*b.b,a.a*b.b+a.b*b.a};}

int n,m;
int l,rev[nmax];
void dft(tcpx *c,int n,int fl){
    rep(i,0,n-1)if(i<rev[i])swap(c[i],c[rev[i]]);
    for(int i=1;i<n;i<<=1){
        tcpx wn=(tcpx){cos(pi/i),fl*sin(pi/i)};
        for(int j=0,p=(i<<1);j<n;j+=p){
            tcpx w=(tcpx){1,0};
            for(int k=0;k<i;++k,w=w*wn){
                tcpx x=c[j+k],y=w*c[j+k+i];
                c[j+k]=x+y,c[j+k+i]=x-y;
            }
        }
    }
}
void fft(tcpx *c1,int n,tcpx *c2,int m,tcpx *c3){//c3=c1*c2
    m+=n;
    for(n=1;n<=m;n<<=1)++l;
    rep(i,0,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
    dft(c1,n,1),dft(c2,n,1);
    rep(i,0,n-1)c3[i]=c1[i]*c2[i];
    dft(c3,n,-1);
    rep(i,0,n-1)c3[i].a=(int)(c3[i].a/n+0.5);
}

[模板] 快速傅里叶变换/fft

标签:傅里叶变换   operator   return   void   for   快速   a+b   swap   swa   

原文地址:https://www.cnblogs.com/ubospica/p/10279351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!