bzoj 2194 快速傅立叶之二
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2194
因为卷积的第 k 项是 sigma(i=0~k)a[ i ]*b[ k-i ] ,也就是角标加起来是 k 的两项求和;所以先把 a 序列翻转一下,然后发现正常卷积的第 n-1-k 项就是 c[k] 。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define db double using namespace std; const int N=1e5+5,M=N<<2; const db pi=acos(-1); int n,r[M],len; struct cpl{db x,y;}a[M],b[M],I; cpl operator+ (cpl a,cpl b){return (cpl){a.x+b.x,a.y+b.y};} cpl operator- (cpl a,cpl b){return (cpl){a.x-b.x,a.y-b.y};} cpl operator* (cpl a,cpl b){return (cpl){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};} int rdn() { int ret=0;bool fx=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();} while(ch>='0'&&ch<='9') ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar(); return fx?ret:-ret; } void fft(cpl *a,bool fx) { for(int i=0;i<len;i++) if(i<r[i])swap(a[i],a[r[i]]); for(int R=2;R<=len;R<<=1) { int m=R>>1; cpl Wn=(cpl){ cos(pi/m),fx?-sin(pi/m):sin(pi/m) }; for(int i=0;i<len;i+=R) { cpl w=I; for(int j=0;j<m;j++,w=w*Wn) { cpl tmp=w*a[i+m+j]; a[i+m+j]=a[i+j]-tmp; a[i+j]=a[i+j]+tmp; } } } } int main() { n=rdn(); I.x=1; for(int i=0;i<n;i++) a[n-1-i].x=rdn(),b[i].x=rdn(); len=1; for(;len<=n<<1;len<<=1); for(int i=0;i<len;i++) r[i]=(r[i>>1]>>1)+((i&1)?len>>1:0); fft(a,0); fft(b,0); for(int i=0;i<len;i++)a[i]=a[i]*b[i]; fft(a,1); for(int i=n-1;i>=0;i--) printf("%d\n",int(a[i].x/len+0.5)); return 0; }