多项式求逆
已知多项式 f(x),试求 f−1(x)。其中,f(x)f−1(x)≡1(modxn)。
首先,根据定义,得 [x0]f(x)[x0]f−1(x)=1,即 [x0]f−1(x)=[x0]f(x)1。
若我们已经得到 f0−1(x) 满足 f(x)f0−1(x)≡1(modx2n),考虑如何计算 f−1(x)。
根据定义,我们有:
f(x)f0−1(x)f(x)f−1(x)≡1(modx2n)≡1(modx2n)
所以
f−1(x)−f0−1(x)f−2(x)−2f−1(x)f0−1(x)+f0−2(x)f−1(x)−2f0−1(x)+f(x)f0−2(x)≡0(modx2n)≡0(modxn)≡0(modxn)
最终得到:
f−1(x)≡2f0−1(x)−f(x)f0−2(x)(modxn)
使用NTT求解即可,时间复杂度为 O(nlogn)。
实现代码
首先是一些封装的函数:
C++void poly_mul(int *f,int *g,int size,int *ans)
{
for(int i=0;i<size<<1;i++)
x[i]=(i<size?f[i]:0);
for(int i=0;i<size<<1;i++)
y[i]=(i<size?g[i]:0);
NTT(x,size<<1,1);
NTT(y,size<<1,1);
for(int i=0;i<size<<1;i++)
ans[i]=1ll*x[i]*y[i]%mod;
NTT(ans,size<<1,-1);
}
void poly_times(int *f,int x,int size,int *ans)
{
for(int i=0;i<size;i++)
ans[i]=1ll*f[i]*x%mod;
}
void poly_sub(int *f,int *g,int size,int *ans)
{
for(int i=0;i<size;i++)
ans[i]=(f[i]+mod-g[i])%mod;
}
然后是主体代码(十分简洁):
代码size=1;
g[0]=qmi(f[0],mod-2);
while(size<=n)
{
size<<=1;
poly_times(g,2,size,u);
poly_mul(f,g,size,t);
poly_mul(t,g,size,p);
poly_sub(u,p,size,g);
}
例题
多项式对数函数
已知 f(x),求 g(x)≡lnf(x)(modxn)。
首先,根据定义,得 [x0]g(x)=ln[x0]f(x)。显然,当且仅当 [x0]f(x)=1 时该值收敛,此时 [x0]g(x)=0。
其次,我们对 lnf(x) 求导再积分,可得:
dxdlnf(x)lnf(x)=f(x)f′(x)=∫dlnf(x)=∫f(x)f′(x)dx
使用多项式求逆即可解决,时间复杂度 O(nlogn)。
实现代码
首先是一些封装的函数:
C++void inv(int *f,int size,int *ans)
{
int now;
now=1;
ans[0]=qmi(f[0],mod-2);
while(now<size)
{
now<<=1;
poly_times(ans,2,now,u);
poly_mul(f,ans,now,t);
poly_mul(t,ans,now,t);
poly_sub(u,t,now,ans);
}
}
void diff(int *f,int size,int *ans)
{
for(int i=0;i<size-1;i++)
ans[i]=1ll*(i+1)*f[i+1]%mod;
ans[size-1]=0;
}
void integ(int *f,int size,int *ans)
{
for(int i=size-1;i>=1;i--)
ans[i]=1ll*qmi(i,mod-2)*f[i-1]%mod;
ans[0]=0;
}
然后是主体代码(依旧十分简洁):
代码size=1;
while(size<n)
size<<=1;
inv(f,size,p);
diff(f,size,f);
poly_mul(f,p,size,g);
integ(g,size,g);
例题
多项式指数函数
已知 f(x),求 g(x)≡ef(x)(modxn)。
首先,根据定义,得 [x0]g(x)=e[x0]f(x)。显然,当且仅当 [x0]f(x)=0 时该值收敛,此时 [x0]g(x)=1。
设 F(g(x))=lng(x)−f(x),则 F(g(x))≡0(modxn)。
若我们已经得到 F(g0(x))≡0(modx2n),考虑如何求出 g(x)。
将 F(g(x)) 在 g0(x) 处泰勒展开,得:
F(g(x))=i=0∑∞i!F(i)(g0(x))(g(x)−g0(x))i
根据定义,得 g(x)−g0(x)≡0(modx2n),所以 (g(x)−g0(x))2≡0(modxn)。
代入上式,得
F(g0(x))+F′(g0(x))(g(x)−g0(x))≡F(g(x))(modxn)
即
F(g0(x))+F′(g0(x))(g(x)−g0(x))≡0(modxn)
对上式变形得:
g(x)≡g0(x)−F′(g0(x))F(g0(x))≡g0(x)−g0(x)1lng0(x)−f(x)≡g0(x)−g0(x)(lng0(x)−f(x))≡g0(x)(1−lng0(x)+f(x))(modxn)
时间复杂度:O(nlogn)。
实现代码
首先是一些封装的函数:
C++void poly_add(int *f,int *g,int size,int *ans)
{
for(int i=0;i<size;i++)
ans[i]=(f[i]+g[i])%mod;
}
void poly_ln(int *f,int size,int *ans)
{
int now;
now=1;
while(now<size)
now<<=1;
for(int i=0;i<now;i++)
o[i]=f[i];
inv(o,now,p);
diff(o,now,o);
poly_mul(o,p,now,ans);
integ(ans,now,ans);
}
然后是主体代码:
代码one[0]=1;
size=1;
g[0]=1;
while(size<n)
{
size<<=1;
poly_ln(g,size,s);
poly_times(s,-1,size,s);
poly_add(one,s,size,s);
poly_add(s,f,size,s);
poly_mul(g,s,size,g);
}
例题
习题
多项式开方
已知 f(x),求 g(x) 满足 g2(x)≡f(x)(modxn)。
首先,根据定义,得 [x0]g(x)=±[x0]f(x)。
若我们已经得到 g02(x)≡f(x)(modx2n),考虑如何求出 g(x)。
注意到有如下式子:
g02(x)−f(x)(g02(x)−f(x))2(g02(x)+f(x))2(2g0(x)g02(x)+f(x))22g0(x)g02(x)+f(x)2g0(x)+g0−1(x)f(x)≡0(modx2n)≡0(modxn)≡4g02(x)f(x)(modxn)≡f(x)(modxn)≡g(x)(modxn)≡g(x)(modxn)
时间复杂度:O(nlogn)。
实现代码
代码size=1;
inv2=qmi(2,mod-2);
g[0]=1;
while(size<n)
{
size<<=1;
inv(g,size,p);
poly_mul(p,f,size,p);
poly_add(g,p,size,g);
poly_times(g,inv2,size,g);
}
例题
多项式除法
已知 f(x),g(x),求 f(x) 除以 g(x) 的商 Q(x) 及余数 R(x)。
考虑构造变换:
fR(x)=xdegf−1f(x1)
其中,degf 表示 f(x) 的次数。
注意到该变换实质为反转 f(x) 的系数。
设 n=degf,m=degg。
根据定义,得:
f(x)xn−1f(x1)fR(x)=Q(x)g(x)+R(x)=xn−mQ(x1)xm−1g(x1)+xn−m+1xm−2R(x1)=QR(x)gR(x)+xn−m+1RR(x)
所以
fR(x)≡QR(x)gR(x)(modxn−m+1)
运用多项式求逆求出 Q(x) 后,通过定义求出 R(x) 即可。时间复杂度:O(nlogn)。
实现代码
C++void reverse(int *f,int size)
{
for(int i=0;i<size-i-1;i++)
swap(f[i],f[size-i-1]);
}
C++reverse(f,n);
reverse(g,m);
inv(g,n-m+1,p);
size=1;
while(size<n-m+1)
size<<=1;
poly_mul(f,p,size,q);
reverse(q,n-m+1);
reverse(f,n);
reverse(g,m);
size=1;
while(size<n)
size<<=1;
for(int i=n-m+1;i<size;i++)
q[i]=0;
poly_mul(q,g,size,p);
poly_sub(f,p,n,r);
例题
多项式三角函数
已知 f(x),求 sinf(x),cosf(x),tanf(x)。
首先,根据 eix=cosx+isinx 得:
sinxcosx=2ieix−e−ix=2eix+e−ix
将 f(x) 代入上式,得:
sinf(x)cosf(x)=2iexp(if(x))−exp(−if(x))=2exp(if(x))+exp(−if(x))
根据上式直接计算即可。(tanf(x)=cosf(x)sinf(x))
运用NTT时,若取 p=998244353,则一般取 i=86583718。
时间复杂度:O(nlogn)。
实现代码
C++void poly_exp(int *f,int size,int *ans)
{
int now;
now=1;
one[0]=1;
ans[0]=1;
for(int i=1;i<size;i++)
ans[i]=0;
while(now<size)
{
now<<=1;
poly_ln(ans,now,s);
poly_times(s,-1,now,s);
poly_add(one,s,now,s);
poly_add(s,f,now,s);
poly_mul(ans,s,now,ans);
}
}
type=0 表示 sinf(x),type=1 表示 cosf(x)。
代码if(type==0)
{
poly_exp(f,n,q);
poly_times(f,-1,n,f);
poly_exp(f,n,r);
poly_times(r,-1,n,r);
poly_add(q,r,n,g);
poly_times(g,qmi(2*img,mod-2),n,g);
}
else
{
poly_exp(f,n,q);
poly_times(f,-1,n,f);
poly_exp(f,n,r);
poly_add(q,r,n,g);
poly_times(g,qmi(2,mod-2),n,g);
}
例题
多项式反三角函数
已知 f(x),求 arcsinf(x),arccosf(x),arctanf(x)。
仿照求多项式对数函数的方法,对反三角函数先求导再积分得:
dxdarcsinf(x)arcsinf(x)dxdarccosf(x)arccosf(x)dxdarctanf(x)arctanf(x)=1−f2(x)f′(x)=∫1−f2(x)f′(x)dx=−1−f2(x)f′(x)=−∫1−f2(x)f′(x)dx=1+f2(x)f′(x)=∫1+f2(x)f′(x)dx
根据上式求解即可,时间复杂度:O(nlogn)。
实现代码
C++void poly_sqrt(int *f,int size,int *ans)
{
int now,inv2;
now=1;
inv2=qmi(2,mod-2);
ans[0]=1;
while(now<size)
{
now<<=1;
inv(ans,now,v);
poly_mul(v,f,now,v);
poly_add(ans,v,now,ans);
poly_times(ans,inv2,now,ans);
}
}
type=0 表示 arcsinf(x),type=1 表示 arctanf(x)。
代码size=1;
while(size<n)
size<<=1;
if(type==0)
{
poly_mul(f,f,size,q);
poly_times(q,-1,size,q);
q[0]=(q[0]+1)%mod;
poly_sqrt(q,size,r);
inv(r,size,w);
diff(f,size,g);
poly_mul(g,w,size,g);
integ(g,size,g);
}
else
{
poly_mul(f,f,size,q);
q[0]=(q[0]+1)%mod;
inv(q,size,r);
diff(f,size,w);
poly_mul(r,w,size,g);
integ(g,size,g);
}
例题