Luo Gu P3338 [ZJOI2014] force

Water problems will not be insensitive to the convolution form \ (QwQ \)

Subject to the effect:

Given \ (n-\) number \ (Q_I \) , the definition of \ (F_j \) as follows:
\ [F_j = \ SUM \ limits_ {I <J} \ {q_iq_j FRAC} {(ij of) ^ 2} - \ sum \ limits_ {i> j} \ frac {q_iq_j} {(ij) ^ 2} \]

\(E_i=\frac{f_i}{q_i}\)

Really a little look can know

\(E_i=\frac{f_i}{q_i}=\sum\limits_{i<j}\frac{q_i}{(i-j)^2}-\sum\limits_{i>j}\frac{q_i}{(i-j)^2}\)

We set \ (f [i] = q_i , g [i] = \ frac {1} {i ^ 2} \)

Original formula is changed to
\ [\ sum \ limits_ {i
<j} f [i] g [ij] - \ sum \ limits_ {i> j} f [i] g [ij] \] is equivalent to
\ [\ SUM \ limits_ {i = 0} ^
{j-1} f [i] g [ij] - \ sum \ limits_ {i = j + 1} ^ {n} f [i] g [ij] \] we set \ (f ^ { '} \) of \ (F \) array after inversion, the original formula is equal to
\ [\ sum \ limits_ {i = 0} ^ {j-1} f [i] g [ij] - \ sum \ limits_ {i = 0} ^ {j-1} f ^ { '} [i] g [ij] \]

These two formulas should only convolution I think it's not right \ (qwq \)

#include<bits/stdc++.h>
using namespace std;
namespace red{
#define eps (1e-8)
    inline int read()
    {
        int x=0;char ch,f=1;
        for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
        if(ch=='-') f=0,ch=getchar();
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
        return f?x:-x;
    }
    const int N=5e5+10;
    const double pi=acos(-1.0);
    int n;
    double f[N],rf[N],g[N],ans1[N],ans2[N];
    int limit,len;
    int pos[N];
    struct complex
    {
        double x,y;
        complex(double tx=0,double ty=0){x=tx,y=ty;}
        inline complex operator + (const complex t) const
        {
            return complex(x+t.x,y+t.y);
        }
        inline complex operator - (const complex t) const
        {
            return complex(x-t.x,y-t.y);
        }
        inline complex operator * (const complex t) const
        {
            return complex(x*t.x-y*t.y,x*t.y+y*t.x);
        }
    }a[N],b[N];
    inline void fft(complex *a,int inv)
    {
        for(int i=0;i<limit;++i)
            if(i<pos[i]) swap(a[i],a[pos[i]]);
        for(int mid=1;mid<limit;mid<<=1)
        {
            complex Wn(cos(pi/mid),inv*sin(pi/mid));
            for(int r=mid<<1,j=0;j<limit;j+=r)
            {
                complex w(1,0);
                for(int k=0;k<mid;++k,w=w*Wn)
                {
                    complex x=a[j+k],y=w*a[j+k+mid];
                    a[j+k]=x+y;
                    a[j+k+mid]=x-y;
                }
            }
        }
    }
    inline void work(double *f,double *g,double *ret)
    {
        for(int i=0;i<limit;++i)
        {
            a[i].x=f[i],b[i].x=g[i];
            a[i].y=b[i].y=0;
        }
        fft(a,1);
        fft(b,1);
        for(int i=0;i<limit;++i)a[i]=a[i]*b[i];
        fft(a,-1);
        for(int i=0;i<=n;++i) ret[i]=a[i].x/limit;
    }
    inline void main()
    {
        n=read();
        for(limit=1;limit<=(n<<1);limit<<=1) ++len;
        for(int i=0;i<limit;++i) pos[i]=(pos[i>>1]>>1)|((i&1)<<(len-1));
        for(int i=1;i<=n;++i)
        {
            scanf("%lf",&f[i]);
            g[i]=1.0/i/i;
            rf[i]=f[i];
        }
        reverse(rf+1,rf+n+1);
        work(f,g,ans1);
        work(rf,g,ans2);
        for(int i=1;i<=n;++i) printf("%.3f\n",ans1[i]-ans2[n-i+1]);
    }
}
signed main()
{
    red::main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/knife-rose/p/12037453.html