bzoj 2179 FFT快速傅立叶 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2179
默写板子,注释的是忘记的地方。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef double db; int const xn=(1<<17); db const Pi=acos(-1.0); int n,lim,l,rev[xn],ans[xn]; struct com{db x,y;}a[xn],b[xn]; com operator + (com a,com b){return (com){a.x+b.x,a.y+b.y};} com operator - (com a,com b){return (com){a.x-b.x,a.y-b.y};} com operator * (com a,com b){return (com){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};} int rd() { int ret=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();} while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar(); return f?ret:-ret; } void fft(com *a,int tp) { for(int i=0;i<lim;i++) if(i<rev[i])swap(a[i],a[rev[i]]); for(int mid=1;mid<lim;mid<<=1)//mid<<=1 { com wn=(com){cos(Pi/mid),tp*sin(Pi/mid)}; for(int j=0,len=(mid<<1);j<lim;j+=len) { com w=(com){1,0}; for(int k=0;k<mid;k++,w=w*wn)//< { com x=a[j+k],y=w*a[j+mid+k]; a[j+k]=x+y; a[j+mid+k]=x-y; } } } } int main() { n=rd()-1; for(int i=0,t;i<=n;i++)scanf("%1d",&t),a[i].x=t; for(int i=0,t;i<=n;i++)scanf("%1d",&t),b[i].x=t; lim=1; while(lim<=n+n)lim<<=1,l++; for(int i=0;i<lim;i++) rev[i]=((rev[i>>1]>>1)|((i&1)<<(l-1))); fft(a,1); fft(b,1); for(int i=0;i<lim;i++)a[i]=a[i]*b[i];//<lim fft(a,-1); for(int i=0;i<=n+n;i++)ans[i]=(int)(a[i].x/lim+0.5); for(int i=n+n;i;i--) if(ans[i]>=10)ans[i-1]+=ans[i]/10,ans[i]%=10; int i=0; while(!ans[i])i++; while(i<=n+n)printf("%d",ans[i]),i++; puts(""); return 0; }