1. 程式人生 > >bzoj 2194快速傅立葉之二

bzoj 2194快速傅立葉之二

/**************************************************************
    Problem: 2194
    User: Clare
    Language: C++
    Result: Accepted
    Time:1564 ms
    Memory:11820 kb
****************************************************************/
 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
 
#define N 300010
const double pi=acos(-1);
 
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
int n,m,l;
int Rev[N];
struct Complex{
    double r,i;
    Complex(){}
    Complex(double r_,double i_):r(r_),i(i_){}
    Complex operator + (const Complex &a) const{
        return (Complex){r+a.r,i+a.i};
    }
    Complex operator - (const Complex &a) const{
        return (Complex){r-a.r,i-a.i};
    }
    Complex operator * (const Complex &a) const{
        return (Complex){r*a.r-i*a.i,r*a.i+i*a.r};
    }
}A[N],B[N];
 
void FFT(Complex *a,int kind)
{
    for(int i=0;i<n;i++)
        if(i<Rev[i])swap(a[i],a[Rev[i]]);
    for(int i=1;i<n;i<<=1)
    {
        Complex wn(cos(pi/i),sin(pi/i)*kind);
        for(int len=i<<1,j=0;j<n;j+=len)
        {
            Complex w(1,0);
            for(int k=0;k<i;k++)
            {
                Complex x=a[j+k],y=w*a[j+k+i];
                a[j+k]=x+y;
                a[j+k+i]=x-y;
                w=w*wn;
            }
        }
    }
}
 
int main()
{
    n=read();n--;
    for(int i=0;i<=n;i++)
    {
        A[i].r=read();B[n-i].r=read();
    }
    m=n+n;n=1;
    int L=0;
    while(n<=m)
        n<<=1,L++;
    for(int i=0;i<n;i++)
        Rev[i]=(Rev[i>>1]>>1)|((i&1)<<(L-1));
    FFT(A,1);FFT(B,1);
    for(int i=0;i<n;i++)
        A[i]=A[i]*B[i];
    FFT(A,-1);
    for(int i=0;i<n;i++)
        A[i].r/=n;
    for(int i=m/2;i<=m;i++)
        printf("%d\n",(int)(A[i].r+0.5));
    return 0;
}