多项式 Ln/Exp

\(\text{Problem}:\)【模板】多项式对数函数(多项式 ln)

\(\text{Solution}:\)

引理 \(1\):

在模意义下当且仅当 \([x^{0}]f(x)=1\) 时,\(f(x)\) 有对数多项式。

设 \(C(x)=\ln(x)\),有:

\[B(x)\equiv C(A(x))\pmod {x^{n}} \]

对两边求导,得到:

\[\begin{aligned} B'(x)&\equiv C'(A(x))A'(x)\pmod {x^{n}}\\ B'(x)&\equiv \cfrac{A'(x)}{A(x)}\pmod {x^{n}} \end{aligned} \]

对 \(A\) 求导及求逆后求出 \(B'\),对它求积分就可以得到 \(B\) 了。

\[\begin{aligned} (x^{a})'&=ax^{a-1}\\ \int x^{a}dx&=\cfrac{x^{a+1}}{a+1} \end{aligned} \]

\(\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;
int rev[N],r[24][2];
vector<int> a,F;
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);
}
inline void GetDao(int n,vector<int> &A,vector<int> &B)
{
	for(ri int i=0;i<n-1;i++) A[i]=1ll*B[i+1]*(i+1)%Mod;
	A[n-1]=0;
}
inline void GetJi(int n,vector<int> &A,vector<int> &B)
{
	for(ri int i=1;i<n;i++) A[i]=1ll*B[i-1]*ksc(i,Mod-2)%Mod;
	A[0]=0;
}
inline 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]-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];
}
inline void GetLn(int n,vector<int> &F)
{
	vector<int> A,B;
	A.resize(n), B.resize(n);
	GetDao(n,A,a);
	GetInv(n,B,a);
	NTT(n,n,A,B);
	GetJi(n,F,A);
}
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();
	a.resize(n), F.resize(n);
	for(ri int i=0;i<n;i++) a[i]=read();
	GetLn(n,F);
	for(ri int i=0;i<n;i++) printf("%d ",F[i]);
	puts("");
	return 0;
}

\(\text{Problem}:\)【模板】多项式指数函数(多项式 exp)

\(\text{Solution}:\)

多项式牛顿迭代:

给定多项式 \(F(x)\),求多项式 \(G(x)\) 满足:

\[F(G(x))\equiv 0\pmod {x^{n}} \]

考虑倍增求解。设 \(G_{0}(x)\) 表示模 \(\left\lceil \dfrac{n}{2}\right\rceil\) 意义下的解,将 \(F(G(x))\) 在 \(G_{0}(x)\) 处进行泰勒展开,有:

\[F(G(x))=\sum\limits_{i=0}^{\infty}\cfrac{F^{(i)}(G_{0}(x))}{i!}(G(x)-G_{0}(x))^{i}\equiv 0\pmod {x^{n}} \]

易知 \(G(x)-G_{0}(x)\) 的最低非零项次数为 \(\left\lceil \dfrac{n}{2}\right\rceil\),故 \(\forall i\geq 2\),都有:

\[(G(x)-G_{0}(x))^{i}\equiv 0\pmod {x^{n}} \]

故对于泰勒展开,只需取前两项,得到:

\[\begin{aligned} F(G(x))&\equiv F(G_{0}(x))+F'(G_{0}(x))(G(x)-G_{0}(x))\\ &\equiv0\pmod {x^{n}}\\ G(x)&\equiv G_{0}(x)-\cfrac{F(G_{0}(x))}{F'(G_{0}(x))}\pmod {x^{n}} \end{aligned} \]

对 \(n=1\) 的情况单独求解,即可倍增求出 \(G(x)\)。

回到本题,对同余式两边 \(\ln\),有:

\[\begin{aligned} F(B(x))&\equiv\ln B(x)-A(x)\pmod {x^{n}}\\ F'(B(x))&\equiv\cfrac{1}{B(x)}\pmod {x^{n}} \end{aligned} \]

将其带回牛顿迭代的式子,有:

\[\begin{aligned} B(x)&\equiv B_{0}(x)-B_{0}(x)(\ln B_{0}(x)-A(x))\pmod {x^{n}}\\ &\equiv B_{0}(x)(1+A(x)-\ln B_{0}(x))\pmod {x^{n}} \end{aligned} \]

总时间复杂度 \(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;
int rev[N],r[24][2],iiv[N+5];
vector<int> a,F;
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];
}
void GetDao(int n,vector<int> &A,vector<int> &B)
{
	for(ri int i=0;i<n-1;i++) A[i]=1ll*(i+1)*B[i+1]%Mod;
	A[n-1]=0;
}
void GetJi(int n,vector<int> &A,vector<int> &B)
{
	for(ri int i=1;i<n;i++) A[i]=1ll*B[i-1]*iiv[i]%Mod;
	A[0]=0;
}
void GetLn(int n,vector<int> &F,vector<int> &G)
{
	vector<int> A,B;
	A.resize(n), B.resize(n);
	GetDao(n,A,G);
	GetInv(n,B,G);
	NTT(n,n,A,B);
	GetJi(n,F,A);
}
void GetExp(int n,vector<int> &F)
{
	if(n==1) { F[0]=1; return; }
	GetExp((n+1)/2,F);
	vector<int> G;
	G.resize(n);
	GetLn(n,G,F);
	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]=(a[i]-G[i]+Mod)%Mod; B[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);
	for(ri int i=0;i<n;i++) F[i]=A[i];
}
signed main()
{
	iiv[1]=1;
	for(ri int i=2;i<=N;i++) iiv[i]=1ll*(Mod-Mod/i)*iiv[Mod%i]%Mod;
	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();
	a.resize(n), F.resize(n);
	for(ri int i=0;i<n;i++) a[i]=read();
	GetExp(n,F);
	for(ri int i=0;i<n;i++) printf("%d ",F[i]);
	puts("");
	return 0;
}
上一篇:软件构造复习第六章 抽象数据类型ADT


下一篇:What crusher machine is used for granite crushing?