题意:

给出a,b,计算 C[k]=n1i=ka[i]b[ik] C [ k ] = ∑ i = k n − 1 a [ i ] ∗ b [ i − k ] , n<=1e5 n <= 1 e 5

Solution:

回想多项式乘法: 2n2k=0(k=i+jaibj)xk ∑ k = 0 2 n − 2 ( ∑ k = i + j a i ∗ b j ) x k

如果相乘的两个式子下标和为定值,那么我们就可以使用FFT优化了

但是题目中的式子并不是定值啊

翻转一下b数组,式子就会变化为 n1i=ka[i]b[ni+k]=i+j=n+ka[i]b[j] ∑ i = k n − 1 a [ i ] ∗ b [ n − i + k ] = ∑ i + j = n + k a [ i ] ∗ b [ j ]

这样我们就可以愉快的用FFT进行优化了

代码:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
const double pi=acos(-1);
struct complex{
    double x,y;
    complex(double _x=0.0,double _y=0.0)
    {
        x=_x,y=_y;
    }
    complex operator +(const complex &b)const
    {
        return complex(x+b.x,y+b.y);
    }
    complex operator -(const complex &b)const
    {
        return complex(x-b.x,y-b.y);
    }
    complex operator *(const complex &b)const
    {
        return complex(x*b.x-y*b.y,x*b.y+y*b.x);
    } 
}x1[300010],x2[300010];
int n,a[100010],b[100010];
void change(complex y[],int len)
{
    int i,j,k;
    j=len/2;
    for (i=1;i<len-1;i++)
    {
        if (i<j) swap(y[i],y[j]);
        k=len/2;
        while (j>=k) j-=k,k>>=1;
        if (j<k) j+=k;
    }
    return;
}
void fft(complex y[],int len,int ifi)
{
    change(y,len);
    for (int h=2;h<=len;h*=2)
    {
        complex wn(cos(ifi*2*pi/h),sin(ifi*2*pi/h));
        for (int j=0;j<len;j+=h)
        {
            complex w(1,0);
            for (int k=j;k<j+h/2;k++)
            {
                complex u=y[k];
                complex t=w*y[k+h/2];
                y[k]=u+t;
                y[k+h/2]=u-t;
                w=w*wn;
            }
        }
    }
    if (ifi==-1) for (int i=0;i<len;i++) y[i].x/=len; 
}
int main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++) scanf("%d%d",&a[i],&b[n-i-1]);
    int len=1;
    while (len<n*2) len*=2;
    for (int i=0;i<n;i++) x1[i]=complex(a[i],0);
    for (int i=n;i<len;i++) x1[i]=complex(0,0);
    for (int i=0;i<n;i++) x2[i]=complex(b[i],0);
    for (int i=n;i<len;i++) x2[i]=complex(0,0);
    fft(x1,len,1);fft(x2,len,1);
    for (int i=0;i<len;i++) x1[i]=x1[i]*x2[i];
    fft(x1,len,-1);
    for (int i=n-1;i<2*n-1;i++) printf("%d\n",(int)(x1[i].x+0.5)); 
}