FFT、NTT模板题。
FFT代码:
#include
#define ll long long
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();c!='-'&&(c<'0'||c>'9');c=getchar());
if(c=='-')f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=500005;
const double PI=acos(-1.0);
int n,m,pos[N],ans[N];
complex<double>f1[N],f2[N];
void rev(int k)
{
for(int i=1;i1)?pos[i>>1]>>1|(k>>1):pos[i>>1]>>1;
}
void FFT(complex<double>f[],int len,int on)
{
for(int i=0;iif(ifor(int i=1;i1)
{
complex<double>wn(cos(on*PI/i),sin(on*PI/i));
for(int j=0;j1))
{
complex<double>wi(1,0);
for(int k=j;kcomplex<double>u=f[k],v=wi*f[k+i];
f[k]=u+v,f[k+i]=u-v;
wi*=wn;
}
}
}
if(on==-1)
for(int i=0;ivoid multi(complex<double>f1[],complex<double>f2[])
{
int len=1;
while(len1;
rev(len);
FFT(f1,len,1),FFT(f2,len,1);
for(int i=0;i1);
for(int i=0;iint(f1[i].real()+0.5);
while(!ans[len]&&len>m+n-2)len--;
for(int i=0;i<=len;i++)printf("%d ",ans[i]);
}
int main()
{
//freopen("lx.in","r",stdin);
n=getint()+1,m=getint()+1;
for(int i=0;ifor(int i=0;ireturn 0;
}
NTT代码:
#include
#define ll long long
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();c!='-'&&(c<'0'||c>'9');c=getchar());
if(c=='-')f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=500005,p=998244353,g=3;
int n,m;
int f1[N],f2[N],pos[N],w[20],inv[20];
int Pow(int x,int y)
{
int res=1;
for(;y;y>>=1,x=(ll)x*x%p)
if(y&1)res=(ll)res*x%p;
return res;
}
void rev(int k)
{
for(int i=1;ipos[i]=(i&1)?pos[i>>1]>>1|(k>>1):pos[i>>1]>>1;
}
void NTT(int f[],int len,int on)
{
for(int i=0;iif(i<pos[i])swap(f[i],f[pos[i]]);
for(int i=1,num=1;i1,num++)
{
int wn=(on==1?w[num]:inv[num]);
for(int j=0;j1))
{
int wi=1;
for(int k=j;kint u=f[k],v=(ll)wi*f[k+i]%p;;
f[k]=(u+v)%p,f[k+i]=(u-v+p)%p;
wi=(ll)wi*wn%p;
}
}
}
if(on==-1)
for(int i=0;i*inv[0]%p;
}
void multi(int f1[],int f2[])
{
int len=1,num=0;
while(lenm)
{
len<<=1;
w[++num]=Pow(g,(p-1)/len);
inv[num]=Pow(w[num],p-2);
}
inv[0]=Pow(len,p-2);
rev(len);
NTT(f1,len,1),NTT(f2,len,1);
for(int i=0;i*f2[i]%p;
NTT(f1,len,-1);
while(!f1[len]&&len>n+m-2)len--;
for(int i=0;i<=len;i++)printf("%d ",f1[i]);
}
int main()
{
//freopen("lx.in","r",stdin);
n=getint()+1,m=getint()+1;
for(int i=0;ifor(int i=0;i<m;i++)f2[i]=getint();
multi(f1,f2);
return 0;
}