多项式除法

\(\text{Problem}:\)【模板】多项式除法

\(\text{Solution}:\)

\(\dfrac{1}{x}\) 代入,有:

\[F(\frac{1}{x})=Q(\frac{1}{x})*G(\frac{1}{x})+R(\frac{1}{x}) \]

\(F_{R}(x)\) 表示 \(F(x)\) 系数反转得到的多项式,则对于一个 \(n\) 次多项式 \(F(x)\),有:

\[F_{R}(x)=x^{n}F(\frac{1}{x}) \]

回到我们要推导的式子,在两边同乘 \(x^{n}\),有:

\[\begin{aligned} x^{n}F(\frac{1}{x})&=x^{n}Q(\frac{1}{x})*G(\frac{1}{x})+x^{n}R(\frac{1}{x})\\ F_{R}(x)&=Q_{R}(x)*G_{R}(x)+x^{n-m+1}R_{R}(x) \end{aligned} \]

将等式两边放在模 \(x^{n-m+1}\) 意义下,可以消去 \(R_{R}(x)\) 的影响。而 \(Q(x)\) 的最高项为 \(n-m<n-m+1\),不会受影响。故有:

\[F_{R}(x)\equiv Q_{R}(x)*G_{R}(x)\pmod {x^{n-m+1}} \]

利用多项式求逆可以求出 \(Q(x)\),带回原式即得到 \(R(x)\)。时间复杂度 \(O(n\log n)\)

\(\text{Code}:\)

#include <bits/stdc++.h>
#pragma GCC optimize(3)
//#define int long long
#define ri register
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define is insert
#define es erase
#define vi vector<int>
#define vpi vector<pair<int,int>>
using namespace std; const int N=265010, Mod=998244353;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n,m;
vector<int> a,b,ra,rb,G,Q,R;
int rev[N],r[24][2];
inline int ksc(int x,int p) { int res=1; for(;p;p>>=1, x=1ll*x*x%Mod) if(p&1) res=1ll*res*x%Mod; return res; }
inline void Get_Rev(int T) { for(ri int i=0;i<T;i++) rev[i]=(rev[i>>1]>>1)|((i&1)?(T>>1):0); }
inline void DFT(int T,vector<int> &s,int type)
{
	for(ri int i=0;i<T;i++) if(rev[i]<i) swap(s[i],s[rev[i]]);
	for(ri int i=2,cnt=1;i<=T;i<<=1,cnt++)
	{
		int wn=r[cnt][type];
		for(ri int j=0,mid=(i>>1);j<T;j+=i)
		{
			for(ri int k=0,w=1;k<mid;k++,w=1ll*w*wn%Mod)
			{
				int x=s[j+k], y=1ll*w*s[j+mid+k]%Mod;
				s[j+k]=(x+y)%Mod;
				s[j+mid+k]=x-y;
				if(s[j+mid+k]<0) s[j+mid+k]+=Mod;
			}
		}
	}
	if(!type) for(ri int i=0,inv=ksc(T,Mod-2);i<T;i++) s[i]=1ll*s[i]*inv%Mod;
}
inline void NTT(int n,int m,vector<int> &A,vector<int> &B)
{
	int len=n+m;
	int T=1;
	while(T<=len) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=n+1;i<T;i++) A[i]=0;
	for(ri int i=m+1;i<T;i++) B[i]=0;
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=1ll*A[i]*B[i]%Mod;
	DFT(T,A,0);
}
void GetInv(int n,vector<int> &F,vector<int> &G)
{
	if(n==1) { F[0]=ksc(G[0],Mod-2); return; }
	GetInv((n+1)/2,F,G);
	vector<int> A,B;
	int T=1;
	while(T<=n+n) T<<=1;
	Get_Rev(T);
	A.resize(T), B.resize(T);
	for(ri int i=0;i<n;i++) A[i]=F[i], B[i]=G[i];
	DFT(T,A,1), DFT(T,B,1);
	for(ri int i=0;i<T;i++) A[i]=(2ll*A[i]%Mod-1ll*B[i]*A[i]%Mod*A[i]%Mod+Mod)%Mod;
	DFT(T,A,0);
	for(ri int i=0;i<n;i++) F[i]=A[i];
}
signed main()
{
	r[23][1]=ksc(3,119), r[23][0]=ksc(ksc(3,Mod-2),119);
	for(ri int i=22;~i;i--) r[i][0]=1ll*r[i+1][0]*r[i+1][0]%Mod, r[i][1]=1ll*r[i+1][1]*r[i+1][1]%Mod;
	n=read(), m=read();
	a.resize(n+1), b.resize(m+1);
	ra.resize(n+1), rb.resize(m+1);
	for(ri int i=0;i<=n;i++) a[i]=ra[n-i]=read();
	for(ri int i=0;i<=m;i++) b[i]=rb[m-i]=read();
	G.resize(n-m+1), rb.resize(n-m+1);
	GetInv(n-m+1,G,rb);
	NTT(n-m+1,n,G,ra);
	Q.resize(n-m+1);
	for(ri int i=0;i<=n-m;i++) Q[i]=G[n-m-i], printf("%d ",Q[i]);
	puts("");
	NTT(n-m+1,m,Q,b);
	R.resize(m);
	for(ri int i=0;i<m;i++) R[i]=(a[i]-Q[i]+Mod)%Mod, printf("%d ",R[i]);
	puts("");
	return 0;
}
posted @ 2021-04-21 10:27  zkdxl  阅读(266)  评论(0编辑  收藏  举报