洛谷 P3338 [ZJOI2014]力 fft

题目描述

洛谷 P3338

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

代码:

// luogu-judger-enable-o2
#include 
#include 
#include 

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;iif (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;j1,0};
            for (int k=0;k2;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>1]>>1)|((i&1)<<(p-1)));
    fft(a,1); fft(b,1); fft(c,1);
    for (int i=0;i1); fft(b,-1);
    for (int i=0;ifor (int i=1;i<=n;i++) printf("%.3lf\n",a[i].x-b[n-i+1].x);
}

你可能感兴趣的:(fft-ntt)