多项式除法(取模)
已知次数为
n
n
n的
A
(
x
)
A(x)
A(x),次数为
m
m
m的
B
(
x
)
B(x)
B(x),求使得
A
(
x
)
=
B
(
x
)
C
(
x
)
+
D
(
x
)
A(x)=B(x)C(x)+D(x)
A(x)=B(x)C(x)+D(x)的
C
(
x
)
,
D
(
x
)
C(x),D(x)
C(x),D(x)
由题
C
(
x
)
C(x)
C(x)的次数为
n
−
m
n-m
n−m,
D
(
x
)
D(x)
D(x)的次数为
m
−
1
m-1
m−1
考虑将系数颠倒过来。
记
A
R
(
x
)
=
x
n
A
(
1
x
)
A^R(x)=x^nA(\frac{1}{x})
AR(x)=xnA(x1)
x
n
A
(
1
x
)
=
x
m
B
(
1
x
)
x
n
−
m
C
(
1
x
)
+
x
n
−
m
+
1
x
m
−
1
D
(
1
x
)
A
R
(
x
)
=
B
R
(
x
)
C
R
(
x
)
+
x
n
−
m
+
1
D
R
(
x
)
A
R
(
x
)
≡
B
R
(
x
)
C
R
(
x
)
(
m
o
d
x
n
−
m
+
1
)
C
R
(
x
)
≡
B
R
(
x
)
−
1
A
R
(
x
)
(
m
o
d
x
n
−
m
+
1
)
x^nA(\frac{1}{x})=x^mB(\frac{1}{x})x^{n-m}C(\frac{1}{x})+x^{n-m+1}x^{m-1}D(\frac{1}{x})\\ A^R(x)=B^R(x)C^R(x)+x^{n-m+1}D^R(x)\\ A^R(x)\equiv{B^R(x)C^R(x)}\pmod{x^{n-m+1}}\\ C^R(x)\equiv{B^R(x)^{-1}A^R(x)}\pmod{x^{n-m+1}}\\
xnA(x1)=xmB(x1)xn−mC(x1)+xn−m+1xm−1D(x1)AR(x)=BR(x)CR(x)+xn−m+1DR(x)AR(x)≡BR(x)CR(x)(modxn−m+1)CR(x)≡BR(x)−1AR(x)(modxn−m+1)
多项式求逆就可解决
代码 P4512
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int g=3;
const int mod=998244353;
const int M=2100009;
int read(){
int f=1,re=0;char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-'){f=-1,ch=getchar();}
for(;isdigit(ch);ch=getchar()) re=(re<<3)+(re<<1)+ch-'0';
return re*f;
}
int ksm(int a,int b){
int ans=1;
while(b){
if(b&1) ans=(ll)ans*a%mod;
a=(ll)a*a%mod;
b>>=1;
}return ans%mod;
}
int n,m,r[M],aa[M],bb[M],a[M],b[M],invb[M],c[M],d[M],tmp[M],aaa[M],bbb[M];
void ntt(int *A,int lim,int type){
for(int i=0;i<lim;i++) if(i<r[i]) swap(A[i],A[r[i]]);
for(int mid=1;mid<lim;mid<<=1){
int W=ksm(g,(mod-1)/(mid<<1));
for(int R=mid<<1,j=0;j<lim;j+=R){
int w=1;
for(ll k=0;k<mid;k++,w=(ll)w*W%mod){
int x=A[j+k],y=(ll)w*A[j+k+mid]%mod;
A[j+k]=(x+y)%mod;
A[j+mid+k]=(x-y+mod)%mod;
}
}
}
if(type==-1){
reverse(A+1,A+lim);
int inv=ksm(lim,mod-2);
for(int i=0;i<lim;i++) A[i]=(ll)A[i]*inv%mod;
}
}
void getinv(int a[],int b[],int len){
if(len==1){
b[0]=ksm(a[0],mod-2);
return;
}getinv(a,b,(len+1)>>1);
int lim=1,l=0;
while(lim<len+len) lim<<=1,l++;
for(int i=0;i<lim;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
for(int i=0;i<len;i++) tmp[i]=a[i];
for(int i=len;i<lim;i++) tmp[i]=0;
ntt(tmp,lim,1),ntt(b,lim,1);
for(int i=0;i<lim;i++) b[i]=(ll)b[i]*((2-(ll)tmp[i]*b[i]%mod+mod)%mod)%mod;
ntt(b,lim,-1);
for(int i=len;i<lim;i++) b[i]=0;
}
void getmul(int a[],int b[],int c[],int n,int m){
int lim=1,l=0;
while(lim<=n+m) lim<<=1,l++;
for(int i=1;i<lim;i++) r[i]=(r[i>>1]>>1|(i&1)<<(l-1));
for(int i=0;i<=n;i++) aa[i]=a[i];
for(int i=0;i<=m;i++) bb[i]=b[i];
ntt(aa,lim,1),ntt(bb,lim,1);
for(int i=0;i<lim;i++){
c[i]=(ll)aa[i]*bb[i]%mod;
aa[i]=bb[i]=0;
}ntt(c,lim,-1);
}
void getdiv(int a[],int b[],int c[],int d[],int n,int m){
for(int i=0;i<=n;i++) aaa[i]=a[i];
for(int i=0;i<=m;i++) bbb[i]=b[i];
reverse(aaa,aaa+n+1),reverse(bbb,bbb+m+1);
getinv(bbb,invb,n-m+1);
getmul(aaa,invb,c,n-m,n-m);
reverse(c,c+n-m+1);
reverse(aaa,aaa+n+1);reverse(bbb,bbb+m+1);
getmul(c,bbb,d,n-m,m);
for(int i=0;i<m;i++) d[i]=(aaa[i]-d[i]+mod)%mod;
for(int i=0;i<=n;i++) aaa[i]=0;
for(int i=0;i<=m;i++) bbb[i]=0;
}
signed main(){
n=read(),m=read();
for(int i=0;i<=n;i++) a[i]=read();
for(int i=0;i<=m;i++) b[i]=read();
getdiv(a,b,c,d,n,m);
for(int i=0;i<=n-m;i++) printf("%d ",c[i]);
printf("\n");
for(int i=0;i<m;i++) printf("%d ",d[i]);
printf("\n");
return 0;
}