【Luogu4512】多项式除法（FFT）

题解

\begin{aligned} A(x)&=B(x)*C(x)+R(x)\\ A(\frac{1}{x})&=B(\frac{1}{x})*C(\frac{1}{x})+R(\frac{1}{x})\\ x^nA(\frac{1}{x})&=(x^m*B(\frac{1}{x}))*(x^{n-m}*C(\frac{1}{x}))+x^nR(\frac{1}{x})\\ A^R(x)&=B^R(x)*C^R(x)+R^R(x)*x^{n-m+1} \end{aligned}

\begin{aligned} A^R(x)&\equiv B^R(x)*C^R(x)+R^R(x)*x^{n-m+1}\\ &\equiv B^R(x)*C^R(x) \end{aligned}

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MOD 998244353
#define MAX 300000
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int fpow(int a,int b)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
int r[MAX],W[MAX];
int N,n,m,NN;
void NTT(int *P,int opt,int N)
{
int l=0;for(int i=1;i<N;i<<=1)++l;
for(int i=0;i<N;++i)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
for(int i=0;i<N;++i)if(i<r[i])swap(P[i],P[r[i]]);
for(int i=1;i<N;i<<=1)
{
int w=fpow(3,(MOD-1)/(i<<1));W[0]=1;
for(int k=1;k<i;++k)W[k]=1ll*W[k-1]*w%MOD;
for(int p=i<<1,j=0;j<N;j+=p)
for(int k=0;k<i;++k)
{
int X=P[j+k],Y=1ll*W[k]*P[i+j+k]%MOD;
P[j+k]=(X+Y)%MOD;P[i+j+k]=(X-Y+MOD)%MOD;
}
}
if(opt==-1)
{
reverse(&P[1],&P[N]);
for(int i=0,inv=fpow(N,MOD-2);i<N;++i)P[i]=1ll*P[i]*inv%MOD;
}
}
int A[MAX],B[MAX];
void Inv(int *a,int *b,int len)
{
if(len==1){b[0]=fpow(a[0],MOD-2);return;}
Inv(a,b,len>>1);
for(int i=0;i<len;++i)A[i]=a[i],B[i]=b[i];
NTT(A,1,len<<1);NTT(B,1,len<<1);
for(int i=0;i<len<<1;++i)A[i]=1ll*A[i]*B[i]%MOD*B[i]%MOD;
NTT(A,-1,len<<1);
for(int i=0;i<len;++i)b[i]=(b[i]+b[i])%MOD;
for(int i=0;i<len;++i)b[i]=(b[i]+MOD-A[i])%MOD;
for(int i=0;i<len<<1;++i)A[i]=B[i]=0;
}
int F[MAX],G[MAX],InvG[MAX];
int Q[MAX],R[MAX];
int main()
{
reverse(&F[0],&F[n+1]);reverse(&G[0],&G[m+1]);
for(NN=1;NN<=n-m+1;NN<<=1);Inv(G,InvG,NN);
for(N=1;N<=n+NN;N<<=1);
NTT(InvG,1,N);NTT(F,1,N);
for(int i=0;i<N;++i)Q[i]=1ll*F[i]*InvG[i]%MOD;
NTT(InvG,-1,N);NTT(F,-1,N);NTT(Q,-1,N);
for(int i=n-m+1;i<N;++i)Q[i]=0;
reverse(&F[0],&F[n+1]);reverse(&G[0],&G[m+1]);reverse(&Q[0],&Q[n-m+1]);
for(int i=0;i<=n-m;++i)printf("%d ",Q[i]);puts("");
for(N=1;N<=n;N<<=1);
NTT(G,1,N);NTT(Q,1,N);
for(int i=0;i<N;++i)G[i]=1ll*G[i]*Q[i]%MOD;
NTT(G,-1,N);
for(int i=0;i<N;++i)R[i]=(F[i]+MOD-G[i])%MOD;
for(int i=0;i<m;++i)printf("%d ",R[i]);puts("");
return 0;

}

posted @ 2018-12-17 09:24  小蒟蒻yyb  阅读(753)  评论(0编辑  收藏  举报