洛谷 3338 [ZJOI2014]力

FFT

qi 除掉之后,发现所有 Ei 的形式很有趣,都差不多,而且像卷积。考虑构造两个多项式来满足 Ei 的答案就是两个多项式相乘的第 i 个系数,乱构一下然后FFT即可。

#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 400005
using namespace std;
namespace runzhe2000
{
    typedef long double ld;
    struct com
    {
        ld r, i;
        com operator + (const com &that) const {return (com){r+that.r, i+that.i};}
        com operator - (const com &that) const {return (com){r-that.r, i-that.i};}
        com operator * (const com &that) const {return (com){r*that.r-i*that.i, r*that.i+i*that.r};}
    }w[N],re_w[N],a[N],b[N];
    int n; ld ans[N], A[N], B[N];
    void init_w()
    {
        const ld pi = acos(-1.0);
        for(int i = 0; i < n; i++) 
        {
            w[i] = (com){cos(2*pi*i/n), sin(2*pi*i/n)};
            re_w[i] = (com){cos(2*pi*i/n), -sin(2*pi*i/n)};
        }
    }
    void FFT(com *a, com *w)
    {
        for(int i = 0, j = 0; i < n; i++)
        {
            if(i > j) swap(a[i], a[j]);
            for(int l = n>>1; (j^=l) < l; l >>= 1);
        }
        for(int i = 2; i <= n; i <<= 1)
        {
            int m = i >> 1;
            for(int j = 0; j < n; j += i)
                for(int k = 0; k < m; k++)
                {
                    com tmp = a[j+k+m] * w[n/i*k];
                    a[j+k+m] = a[j+k] - tmp;
                    a[j+k] = a[j+k] + tmp;
                }
        }
    }
    void main() 
    {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) scanf("%Lf",&A[i]), B[i] = 1.0/i/i;
        int tmp = n; for(n=1; n<=tmp+tmp; n<<=1); init_w();

        for(int i = 0; i < N; i++) a[i].r = a[i].i = b[i].r = b[i].i = 0;
        for(int i = 1; i <= tmp; i++) a[i].r = A[i], b[i].r = B[i];
        FFT(a, w); FFT(b, w); for(int i = 0; i < n; i++) a[i] = a[i] * b[i]; FFT(a, re_w);
        for(int i = 1; i <= tmp; i++) ans[i] += a[i].r / n;

        for(int i = 0; i < n; i++) a[i].r = a[i].i = b[i].r = b[i].i = 0;
        for(int i = 1; i <= tmp; i++) a[i].r = A[tmp-i+1], b[i].r = B[i];
        FFT(a, w); FFT(b, w); for(int i = 0; i < n; i++) a[i] = a[i] * b[i]; FFT(a, re_w);
        for(int i = 1; i <= tmp; i++) ans[i] -= a[tmp-i+1].r / n;

        for(int i = 1; i <= tmp; i++) printf("%.3lf\n",(double)(ans[i]+1e-8));
    }
}
int main()
{
    runzhe2000::main();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值