题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2194

如果把 a 序列翻转,则卷积得到的是 c[n-i],再把得到的 c 序列翻转即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
int const xn=(<<);
db const Pi=acos(-1.0);
int n,lim,l,c[xn],rev[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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void fft(com *a,int tp)
{
for(int i=;i<lim;i++)
if(i<rev[i])swap(a[i],a[rev[i]]);
for(int mid=;mid<lim;mid<<=)
{
com wn=(com){cos(Pi/mid),tp*sin(Pi/mid)};
for(int j=,len=(mid<<);j<lim;j+=len)
{
com w=(com){,};
for(int k=;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()-;
for(int i=;i<=n;i++)a[n-i].x=rd(),b[i].x=rd();
lim=;
while(lim<=n+n)lim<<=,l++;
for(int i=;i<lim;i++)
rev[i]=((rev[i>>]>>)|((i&)<<(l-)));
fft(a,); fft(b,);
for(int i=;i<lim;i++)a[i]=a[i]*b[i];
fft(a,-);
for(int i=;i<=n;i++)c[n-i]=(int)(a[i].x/lim+0.5);
for(int i=;i<=n;i++)printf("%d\n",c[i]);
return ;
}
05-11 14:02