FFT 学习笔记

待填

代码

#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define lod double
using namespace std;
const int n7=3012345;
const lod pie=acos(-1);
int n,m,rv[n7];

struct mle{lod x,y;}a[n7],b[n7];
mle operator + (mle p,mle q){return (mle){p.x+q.x,p.y+q.y};}
mle operator - (mle p,mle q){return (mle){p.x-q.x,p.y-q.y};}
mle operator * (mle p,mle q){return (mle){p.x*q.x-p.y*q.y,p.x*q.y+p.y*q.x};}

int rd(){
	int shu=0;char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))shu=(shu<<1)+(shu<<3)+ch-'0',ch=getchar();
	return shu;
}

void FFT(mle *c,bool sys){
	rep(i,0,n-1)if(i<rv[i])swap(c[i],c[ rv[i] ]);
	for(int len=2;len<=n;len<<=1){
		mle ori=(mle){cos(2*pie/len),(sys?1:-1)*sin(2*pie/len)};
		int le=(len>>1);
		for(int i=0;i<n;i+=len){
			mle z=(mle){1,0};
			rep(j,i,i+le-1){
				mle tmp=z*c[le+j];
				c[le+j]=c[j]-tmp;
				c[j]=c[j]+tmp;
				z=z*ori;
			}
		}
	}
}

int main(){
	n=rd(),m=rd();
	rep(i,0,n)a[i].x=rd();
	rep(i,0,m)b[i].x=rd();
	m=m+n,n=1;
	while(n<=m)n=n<<1;
	rep(i,0,n-1){
		rv[i]=(rv[i>>1]>>1);
		if(i&1)rv[i]=rv[i]|(n>>1);
	}
	FFT(a,1),FFT(b,1);
	rep(i,0,n)a[i]=a[i]*b[i];
	FFT(a,0);
	rep(i,0,m)printf("%d ",(int)(a[i].x/n+0.5));
	return 0;
}

上一篇:Spring-IOC容器的底层原理


下一篇:TWAIN