其他分享
首页 > 其他分享> > 2019.02.28 bzoj3527: [Zjoi2014]力(fft)

2019.02.28 bzoj3527: [Zjoi2014]力(fft)

作者:互联网

传送门
fftfftfft菜题。
题意简述:给一个数列aia_iai​,对于i=1ni=1\rightarrow ni=1→n求出ansi=i&lt;jai(ij)2i&gt;jai(ij)2ans_i=\sum_{i&lt;j}\frac{a_i}{(i-j)^2}-\sum_{i&gt;j}\frac{a_i}{(i-j)^2}ansi​=∑i<j​(i−j)2ai​​−∑i>j​(i−j)2ai​​


思路:
考虑分开求减号前后的两组和。
前面的直接是一个卷积的形式,后面的可以把aaa数组翻转一下也是一个卷积的形式,然后上fftfftfft求即可。
代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+5;
struct cp{
    double x,y;
    cp(double _x=0,double _y=0):x(_x),y(_y){}
    friend inline cp operator+(const cp&a,const cp&b){return cp(a.x+b.x,a.y+b.y);}
    friend inline cp operator-(const cp&a,const cp&b){return cp(a.x-b.x,a.y-b.y);}
    friend inline cp operator*(const cp&a,const cp&b){return cp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
    friend inline cp operator/(const cp&a,const double&b){return cp(a.x/b,a.y/b);}
}a[N];
vector<int>pos;
vector<cp>A,B;
int lim,tim,n;
inline void init(const int&up){
    lim=1,tim=0;
    while(lim<=up)lim<<=1,++tim;
    pos.resize(lim),A.resize(lim),B.resize(lim),pos[0]=0;
    for(ri i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
}
inline void fft(vector<cp>&a,const int&type){
    for(ri i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
    cp wn,w,a0,a1;
    for(ri mid=1;mid<lim;mid<<=1){
        wn=cp(cos(pi/mid),sin(pi/mid)*type);
        for(ri j=0,len=mid<<1;j<lim;j+=len){
            w=cp(1,0);
            for(ri k=0;k<mid;++k,w=w*wn)a0=a[j+k],a1=w*a[j+k+mid],a[j+k]=a0+a1,a[j+k+mid]=a0-a1;
        }
    }
    if(type==-1)for(ri i=0;i<lim;++i)a[i]=a[i]/lim;
}
struct poly{
    vector<cp>a;
    poly(int k=0,cp x=cp(0,0)){a.resize(k+1),a[k]=x;}
    inline cp&operator[](const int&k){return a[k];}
    inline const cp&operator[](const int&k)const{return a[k];}
    inline int deg()const{return a.size()-1;}
    inline poly extend(const int&k){poly ret=*this;return ret.a.resize(k+1),ret;}
    friend inline poly operator*(const poly&a,const poly&b){
        int n=a.deg(),m=b.deg();
        init(n+m);
        for(ri i=0;i<=n;++i)A[i]=a[i];
        for(ri i=0;i<=m;++i)B[i]=b[i];
        for(ri i=n+1;i<lim;++i)A[i]=cp(0,0);
        for(ri i=m+1;i<lim;++i)B[i]=cp(0,0);
        fft(A,1),fft(B,1);
        for(ri i=0;i<lim;++i)A[i]=A[i]*B[i];
        poly ret;
        return fft(A,-1),ret.a=A,ret;
    }
}X,Y;
double ans[2][N];
inline void solve(int type){
    poly X(n),Y(n);
    X[0]=Y[0]=cp(0,0);
    for(ri i=1;i<=n;++i)X[i]=a[i],Y[i]=cp(1.0/(double)i/(double)i,0);
    X=(X*Y).extend(n);
    for(ri i=1;i<=n;++i)ans[type][i]=X[i].x;
}
int main(){
    scanf("%d",&n);
    for(ri i=1;i<=n;++i)scanf("%lf",&a[i].x);
    solve(1),reverse(a+1,a+n+1),solve(0),reverse(ans[0]+1,ans[0]+n+1);
    for(ri i=1;i<=n;++i)printf("%.3lf\n",ans[1][i]-ans[0][i]);
    return 0;
}

标签:fft,const,int,2019.02,28,return,operator,inline,cp
来源: https://blog.csdn.net/dreaming__ldx/article/details/88045821