洛谷 P3338 [ZJOI2014]力 fft

版权声明:2333 https://blog.csdn.net/liangzihao1/article/details/79956958

题目描述

洛谷 P3338

分析:其实很简单啦,就是fft模板题。主要是这样一种形式
f[i]=sum(g[j]*h[i-j])
然后把g*h用fft跑出来,系数就是f[i]

代码:

// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cmath>

const int maxn=3e5+7;
const double pi=acos(-1);

using namespace std;

struct rec{
    double x,y;
};

rec operator + (rec a,rec b)
{
    return (rec){a.x+b.x,a.y+b.y};
}

rec operator - (rec a,rec b)
{
    return (rec){a.x-b.x,a.y-b.y};
}

rec operator * (rec a,rec b)
{
    return (rec){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};
}

int n,m;
rec a[maxn],b[maxn],c[maxn];
int r[maxn],l,p;

void fft(rec *a,int f)
{
    for (int i=0;i<l;i++)
    {
        if (r[i]>i) swap(a[i],a[r[i]]);
    }
    for (int i=2;i<=l;i*=2)
    {
        rec wn=(rec){cos(2*pi/i),f*sin(2*pi/i)};
        for (int j=0;j<l;j+=i)
        {
            rec w=(rec){1,0};
            for (int k=0;k<i/2;k++)
            {
                rec u=a[j+k],v=w*a[j+k+i/2];
                a[j+k]=u+v;
                a[j+k+i/2]=u-v;
                w=w*wn;
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%lf",&a[i].x);
        b[n-i+1].x=a[i].x;
        c[i].x=(double)1/i/i;
    }
    l=1;p=0;
    while (l<=(n*2)) l*=2,p++;
    for (int i=0;i<l;i++) r[i]=((r[i>>1]>>1)|((i&1)<<(p-1)));
    fft(a,1); fft(b,1); fft(c,1);
    for (int i=0;i<l;i++)
    {
        a[i]=a[i]*c[i];
        b[i]=b[i]*c[i];
    }
    fft(a,-1); fft(b,-1);
    for (int i=0;i<l;i++)
    {
        a[i].x=a[i].x/l;
        b[i].x=b[i].x/l;
    }
    for (int i=1;i<=n;i++) printf("%.3lf\n",a[i].x-b[n-i+1].x);
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页