# 【bzoj2194】快速傅立叶之二 FFT

5
3 1
2 4
1 1
2 4
1 4

24
12
10
6
1

FFT

#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 1 << 20
#define pi acos(-1)
using namespace std;
struct data
{
double x , y;
data() {x = y = 0;}
data(double x0 , double y0) {x = x0 , y = y0;}
data operator +(const data a)const {return data(x + a.x , y + a.y);}
data operator -(const data a)const {return data(x - a.x , y - a.y);}
data operator *(const data a)const {return data(x * a.x - y * a.y , x * a.y + y * a.x);}
}a[N] , b[N];
void fft(data *a , int n , int flag)
{
int i , j , k;
for(i = k = 0 ; i < n ; i ++ )
{
if(i > k) swap(a[i] , a[k]);
for(j = (n >> 1) ; (k ^= j) < j ; j >>= 1);
}
for(k = 2 ; k <= n ; k <<= 1)
{
data wn(cos(2 * pi * flag / k) , sin(2 * pi * flag / k));
for(i = 0 ; i < n ; i += k)
{
data t , w(1 , 0);
for(j = 0 ; j < (k >> 1) ; j ++ , w = w * wn)
{
t = w * a[i + j + (k >> 1)];
a[i + j + (k >> 1)] = a[i + j] - t;
a[i + j] = a[i + j] + t;
}
}
}
}
int main()
{
int n , i , len;
scanf("%d" , &n);
for(i = 0 ; i < n ; i ++ ) scanf("%lf%lf" , &a[i].x , &b[n - i - 1].x);
for(len = 1 ; len <= 2 * n ; len <<= 1);
fft(a , len , 1) , fft(b , len , 1);
for(i = 0 ; i < len ; i ++ ) a[i] = a[i] * b[i];
fft(a , len , -1);
for(i = n - 1 ; i <= 2 * n - 2 ; i ++ ) printf("%lld\n" , (long long)(a[i].x / len + 0.1));
return 0;
}

|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-05-19 15:02  GXZlegend  阅读(653)  评论(2编辑  收藏  举报