【BZOJ】【2194】快速傅里叶之二

时间:2022-06-13 21:27:38

FFT

  c[k]=sigma a[i]*b[i-k] 这个形式不好搞……

  而我们熟悉的卷积的形式是这样的 c[k]=sigma a[i]*b[k-i]也就是【下标之和是定值】

  所以我们将a数组反转一下就可以卷积了=。=

 /**************************************************************
Problem: 2194
User: Tunix
Language: C++
Result: Accepted
Time:2008 ms
Memory:48148 kb
****************************************************************/ //BZOJ 2194
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
/*******************template********************/
const int N=;
const double pi=acos(-);
struct comp{
double r,i;
// comp(){}
comp(double _=0.0,double __=0.0) : r(_),i(__){}
comp operator + (const comp &b)const{return comp(r+b.r,i+b.i);}
comp operator - (const comp &b)const{return comp(r-b.r,i-b.i);}
comp operator * (const comp &b)const{return comp(r*b.r-i*b.i,r*b.i+i*b.r);}
}a[N],b[N],c[N];
void FFT(comp *a,int n,int type){
for(int i=,j=;i<n-;++i){
for(int s=n;j^=s>>=,~j&s;);
if(i<j) swap(a[i],a[j]);
}
for(int m=;m<n;m<<=){
double u=pi/m*type; comp wm(cos(u),sin(u));
for(int i=;i<n;i+=(m<<)){
comp w(,);
rep(j,m){
comp &A=a[i+j+m], &B=a[i+j], t=w*A;
A=B-t; B=B+t; w=w*wm;
}
}
}
if (type==-) rep(i,n) a[i].r/=n;
} int main(){
int n=getint();
rep(i,n){
a[n-i-].r=getint();
b[i].r=getint();
}
int len=;
for(len=;len<=n<<;len<<=);
FFT(a,len,); FFT(b,len,);
rep(i,len) c[i]=a[i]*b[i];
FFT(c,len,-);
D(i,n-,) printf("%d\n",int(c[i].r+0.5) );
return ;
}