bzoj3527:[Zjoi2014]力

传送门

\[ F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2}\\ E_j=\frac{\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2}}{q_j}\\ =\sum_{i<j}\frac{q_i}{(i-j)^2}-\sum_{i>j}\frac{q_i}{(i-j)^2} \]

然后设
\[ a_i=\frac{1}{i^2}\\ p_i=q_{n-i+1} \]
那么就有
\[ E_j=\sum_{i=1}^{j-1}q_ia_{j-i}-\sum_{i=j+1}^{n}q_ia_{i-j}\\ E_j=\sum_{i=1}^{j-1}q_ia_{j-i}-\sum_{i=j+1}^{n}p_{n-i+1}a_{i-j}\\ E_j=\sum_{i=1}^{j-1}q_ia_{j-i}-\sum_{i=1}^{n-j}p_{n-j-i+1}a_i\\ \]
然后前后都是卷积的形式,可以用FFT计算了

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=4e5+10;const double pi=acos(-1);
int n,len,m,nn,r[maxn];
struct complex{double x,y;}a[maxn],b[maxn],c[maxn];
complex operator-(complex a,complex b){return (complex){a.x-b.x,a.y-b.y};}
complex operator+(complex a,complex b){return (complex){a.x+b.x,a.y+b.y};}
complex operator*(complex a,complex b){return (complex){a.x*b.x-a.y*b.y,a.y*b.x+b.y*a.x};}
void fft(complex *a,int f)
{
    for(rg int i=0;i<n;i++)if(r[i]>i)swap(a[r[i]],a[i]);
    for(rg int i=1;i<n;i<<=1)
    {
        complex wn=(complex){cos(pi/i),f*sin(pi/i)};
        for(rg int j=0;j<n;j+=(i<<1))
        {
            complex w=(complex){1,0};
            for(rg 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;
            }
        }
    }
    if(f==-1)for(rg int i=0;i<n;i++)a[i].x=a[i].x/n;
}
int main()
{
    read(n),nn=n;
    for(rg int i=1;i<=n;i++)scanf("%lf",&a[i].x),c[i].x=1.00/i/i;
    for(rg int i=1;i<=n;i++)b[i].x=a[n-i+1].x;
    m=n*2;for(n=1;n<=m;n<<=1)len++;
    for(rg int i=0;i<n;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(len-1));
    fft(a,1),fft(c,1),fft(b,1);
    for(rg int i=0;i<n;i++)a[i]=a[i]*c[i],b[i]=b[i]*c[i];
    fft(a,-1),fft(b,-1);
    for(rg int i=1;i<=m>>1;i++)printf("%.3lf\n",a[i].x-b[nn-i+1].x);
}
posted @ 2019-03-30 12:04 蒟蒻--lichenxi 阅读(...) 评论(...) 编辑 收藏