FFT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
#include<cstring>
#include<map>
using namespace std;
#define ll long long
#define R register
#define fo(a,b,c) for(R int a=b;a<=c;++a)
#define mmt(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout<<#x<<" = "<<x<<'\n'
#define maxn 4000100
const double pi=acos(-1);
struct node
{
double r,i;
node(double rr=0,double ii=0){r=rr;i=ii;}
node operator + (const node &a)const{return node(r+a.r,i+a.i);}
node operator - (const node &a)const{return node(r-a.r,i-a.i);}
node operator * (const node &a)const{return node(r*a.r-i*a.i,i*a.r+r*a.i);}
}a[maxn],b[maxn];
int real[maxn],lim=1,L,n,m;
void fft(node *a,int ty)
{
fo(i,0,lim-1)if(i<real[i])swap(a[i],a[real[i]]);
for(int mid=1;mid<lim;mid<<=1)
{
node wn(cos(pi/mid),ty*sin(pi/mid));
for(int j=0;j<lim;j+=(mid<<1))
{
node w(1,0);
for(int k=0;k<mid;++k,w=w*wn)
{
node x=a[j+k],y=w*a[j+mid+k];
a[j+k]=x+y;a[j+mid+k]=x-y;
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
fo(i,0,n)scanf("%lf",&a[i].r);
fo(i,0,m)scanf("%lf",&b[i].r);
while(lim<=n+m)lim<<=1,L++;
fo(i,0,lim-1)real[i]=(real[i>>1]>>1)|((i&1)<<(L-1));
fft(a,1);fft(b,1);
fo(i,0,lim)a[i]=a[i]*b[i];
fft(a,-1);
fo(i,0,m+n)
printf("%d ",(int)(a[i].r/lim+0.5));
return 0;
}
显示 Gitment 评论