BZOJ 3527 力(FFT)

Description

给出一个长度为 n 的序列 q0,...,n1 ,定义 Fj=i<jqiqj(ij)2i>jqiqj(ij)2 ,求 Ei=Fiqi

Input

第一行一个整数 n 表示序列长度,之后输入 n 个实数 q0,...,n1 (n105,0<qi<109)

Output

输出 n 个实数 E0,...,n1 ,结果与标准答案误差不超过 102

Sample Input

5

4006373.885184

15375036.435759

1717456.469144

8514941.004912

1410681.345880

Sample Output

-16838672.693

3439.793

7509018.566

4595686.886

10903040.872

Solution

Ei=j=0i1qj1(ij)2j=i+1n1qj1(ij)2=j=0i1qj1(ij)2j=1ni1qnj1(nij)2

A[i]=qi,B[i]=qni,0i<n,A[n]=B[n]=0,C[i]=1i2,1in,C[0]=0

X[i]=j=0iA[j]C[ij],Y[i]=j=0iB[j]C[ij],0in

做两遍 FFT 得到 X Y ,进而由 Ei=X[i]Y[ni] 求得 E

Code

#include
#include
#include 
using namespace std;
#define maxn 100005
#define maxfft 131072+5
const double pi=acos(-1.0);
struct cp
{
    double a,b;
    cp operator +(const cp &o)const {return (cp){a+o.a,b+o.b};}
    cp operator -(const cp &o)const {return (cp){a-o.a,b-o.b};}
    cp operator *(const cp &o)const {return (cp){a*o.a-b*o.b,b*o.a+a*o.b};}
    cp operator *(const double &o)const {return (cp){a*o,b*o};}
    cp operator !() const{return (cp){a,-b};}
}w[maxfft];
int pos[maxfft];
void fft_init(int len)
{
    int j=0;
    while((1<for(int i=0;ipos[i]=pos[i>>1]>>1|((i&1)<*x,int len,int sta)
{
    for(int i=0;iif(i<pos[i])swap(x[i],x[pos[i]]);
    w[0]=(cp){1,0};
    for(unsigned i=2;i<=len;i<<=1)
    {
        cp g=(cp){cos(2*pi/i),sin(2*pi/i)*sta};
        for(int j=i>>1;j>=0;j-=2)w[j]=w[j>>1];
        for(int j=1;j>1;j+=2)w[j]=w[j-1]*g;
        for(int j=0;j*a=x+j,*b=a+(i>>1);
            for(int l=0;l>1;l++)
            {
                cp o=b[l]*w[l];
                b[l]=a[l]-o;
                a[l]=a[l]+o;
            }
        }
    }
    if(sta==-1)for(int i=0;ix[i].a/=len,x[i].b/=len;
}
cp x[maxfft],y[maxfft],z[maxfft];
void FFT(double *a,double *b,int n,int m,double *c)
{
    int len=1;
    while(len<(n+m)>>1)len<<=1;
    fft_init(len);
    for(int i=n/2;ix[i].a=x[i].b=0;
    for(int i=m/2;i>1].b:x[i>>1].a)=a[i];
    for(int i=0;i>1].b:y[i>>1].a)=b[i];
    fft(x,len,1),fft(y,len,1);
    for(int i=0;i2;i++)
    {
        int j=len-1&len-i;
        z[i]=x[i]*y[i]-(x[i]-!x[j])*(y[i]-!y[j])*(w[i]+(cp){1,0})*0.25;
    }
    for(int i=len/2;iint j=len-1&len-i;
        z[i]=x[i]*y[i]-(x[i]-!x[j])*(y[i]-!y[j])*((cp){1,0}-w[i^len>>1])*0.25;
    }
    fft(z,len,-1);
    for(int i=0;iif(i&1)c[i]=z[i>>1].b;
        else c[i]=z[i>>1].a;
}
int n;
double q[maxn],A[maxn],B[maxn],ans[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i"%lf",&q[i]);
        for(int i=0;iq[i];
        A[n]=0;
        B[0]=0;
        for(int i=1;i<=n;i++)B[i]=1.0/i/i;
        FFT(A,B,n+1,n+1,A);
        for(int i=0;ifor(int i=0;iq[n-i];
        A[n]=0;
        B[0]=0;
        for(int i=1;i<=n;i++)B[i]=1.0/i/i;
        FFT(A,B,n+1,n+1,A);
        for(int i=0;ifor(int i=0;iprintf("%.3f\n",ans[i]);
    }
    return 0;
}

你可能感兴趣的:(BZOJ,FFT)