BZOJ 3527 FFT

https://www.lydsy.com/JudgeOnline/problem.php?id=3527

A easy problem of FFT

You just have a try and do a Multi

The you will find secret of this problem

Code of AC:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
//复数结构体
struct Complex{
    double x,y;//实部和虚部 x+yi
    Complex(double _x = 0.0,double _y = 0.0){
        x = _x;
        y = _y;
    }
    Complex operator -(const Complex &b)const{
        return Complex(x-b.x,y-b.y);
    }
    Complex operator +(const Complex &b)const{
        return Complex(x+b.x,y+b.y);
    }
    Complex operator *(const Complex &b)const{
        return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
    }
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
void change(Complex y[],int len)
{
    int i,j,k;
    for(i = 1, j = len/2; i <len-1; i++)
    {
        if(i < j)swap(y[i],y[j]);
//交换互为小标反转的元素, i<j保证交换一次
//i做正常的+1, j左反转类型的+1,始终保持i和j是反转的
        k = len/2;
        while(j >= k)
        {
            j -= k;
            k /= 2;
        }
        if(j < k)j += k;
    }
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT, on==-1时是IDFT
*/
void fft(Complex y[],int len,int on)
{
    change(y,len);
    for(int h = 2; h <= len; h <<= 1)
    {
        Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
        for(int j = 0; j < len; j+=h)
        {
            Complex w(1,0);
            for(int k = j; k < j+h/2; k++)
            {
                Complex u = y[k];
                Complex t = w*y[k+h/2];
                y[k] = u+t;
                y[k+h/2] = u-t;
                w = w*wn;
            }
        }
    }
    if(on == -1)
        for(int i = 0; i < len; i++)
            y[i].x /= len;
}
const int N=1e5;
Complex A[N*4+10],B[N*4+10],C[N*4+10],D[N*4+10];
int main(){
    ios::sync_with_stdio(false);  
    cin.tie(0);
    int n;
    cin>>n;
    int len=1;
    while(len<2*n) len<<=1;
    for(ll i=1;i<n;++i) A[i]=1.0/(i*i);
    for(int i=0;i<n;++i) cin>>B[i].x,C[n-i-1].x=B[i].x;
    fft(A,len,1);
    fft(B,len,1);
    fft(C,len,1);
    for(int i=0;i<len;++i){
        B[i]=A[i]*B[i];
        C[i]=A[i]*C[i];
    }
    fft(B,len,-1);
    fft(C,len,-1);
    for(int i=0;i<n;++i)
        printf("%.10lf\n",B[i].x-C[n-i-1].x);
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80530410