# [模板] MTT实现之FFT拆系数

## 题目分析：

P不保证分解成那个形式，那么我们就不可以用NTT了.

Luogu 4245

## Ac 代码：

#include<cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define double long double
const double PI=std::acos(-1);
const int maxm=5e5+100,M=32768;
struct complex{
double real,imag;
complex(){};
complex(double _real,double _imag):real(_real),imag(_imag){}
};
inline complex operator + (complex x,complex y){return (complex){x.real+y.real,x.imag+y.imag};}
inline complex operator - (complex x,complex y){return (complex){x.real-y.real,x.imag-y.imag};}
inline complex operator * (complex x,complex y){return (complex){x.real*y.real-x.imag*y.imag,x.real*y.imag+y.real*x.imag};}
int F[maxm],G[maxm],rev[maxm],l;
inline void FFT(complex *a,int n,int f)
{
for(int i=0;i<n;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
for(int i=0;i<n;i++) if(i<rev[i]) std::swap(a[i],a[rev[i]]);
for(int i=1;i<n;i<<=1)
{
complex wn=(complex){std::cos(PI/i),f*std::sin(PI/i)};
for(int j=0;j<n;j+=(i<<1))
{
complex w=(complex){1,0};
for(int k=0;k<i;k++,w=w*wn)
{
complex x=a[j+k];
complex y=a[i+j+k]*w;
a[j+k]=x+y;
a[i+j+k]=x-y;
}
}
}
if(f==1) return;
for(int i=0;i<n;i++) a[i].real=a[i].real/(double)n;
}
int n,m,p;
ll ans[maxm];
complex a[maxm],b[maxm],c[maxm],d[maxm],e[maxm],f[maxm],g[maxm],h[maxm];
inline void MTT()
{
n+=m;
for(m=1;m<=n;m<<=1) l++;
for(int i=0;i<n;i++)
{
a[i].real=F[i]/M,b[i].real=F[i]%M;
c[i].real=G[i]/M,d[i].real=G[i]%M;
}
FFT(a,m,1),FFT(b,m,1),FFT(c,m,1),FFT(d,m,1);
for(int i=0;i<m;i++)
{
e[i]=a[i]*c[i],f[i]=a[i]*d[i];
g[i]=b[i]*c[i],h[i]=b[i]*d[i];

}
FFT(e,m,-1),FFT(f,m,-1),FFT(g,m,-1),FFT(h,m,-1);
for(int i=0;i<m;i++)
{
ans[i]=(ll)(round(e[i].real))%p*M%p*M%p;
ans[i]+=(ll)(round(f[i].real))%p*M%p;
ans[i]+=(ll)(round(g[i].real))%p*M%p;
ans[i]+=(ll)(round(h[i].real))%p;
ans[i]%=p;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
for(int i=0;i<=n;i++) scanf("%d",&F[i]);
for(int i=0;i<=m;i++) scanf("%d",&G[i]);
MTT();
for(int i=0;i<=n;i++) printf("%lld ",ans[i]);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120