多项式开根
\(\text{Problem}:\)【模板】多项式开根
\(\text{Solution}:\)
给定多项式 \(A(x)\),求 \(B(x)\),满足:
\[B^{2}(x)\equiv A(x)\pmod {x^{n}}
\]
考虑倍增求解。设 \(B_{0}(x)\) 表示模 \(\left\lceil \dfrac{n}{2} \right\rceil\) 意义下的解,有:
\[\begin{aligned}
B_{0}^{2}(x)-A(x)&\equiv 0\pmod {x^{\left\lceil \frac{n}{2} \right\rceil}}\\
(B_{0}^{2}(x)-A(x))^{2}&\equiv 0\pmod {x^{n}}\\
(B_{0}^{2}(x)+A(x))^{2}&\equiv4B_{0}^{2}(x)A(x)\pmod {x^{n}}\\
\left(\cfrac{B_{0}^{2}(x)+A(x)}{2B_{0}(x)}\right)^{2}&\equiv A(x)\pmod {x^{n}}\\
\cfrac{B_{0}^{2}(x)+A(x)}{2B_{0}(x)}&\equiv B(x)\pmod {x^{n}}
\end{aligned}
\]
只需对 \(B_{0}(x)\) 求逆即可求出 \(B(x)\)。时间复杂度 \(O(n\log n)\)。
注意当 \([x^{0}]A(x)\not =1\) 时,使用二次剩余计算 \([x^{0}]B(x)\)。
\(\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,I2;
vector<int> a,F;
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];
}
struct Node { int x,y; };
inline Node operator * (Node a,Node b)
{
int w1=(1ll*a.x*b.x%Mod+1ll*a.y*b.y%Mod*I2%Mod)%Mod;
int w2=(1ll*a.x*b.y%Mod+1ll*a.y*b.x%Mod)%Mod;
return (Node){w1,w2};
}
inline bool Check(int x)
{
return ksc(x,(Mod-1)/2)==1;
}
inline int Rand()
{
int w=1ll*rand()*rand()%Mod;
w+=rand()-rand();
w=(w%Mod+Mod)%Mod;
return w;
}
inline Node KSC(Node x,int p) { Node res=(Node){1,0}; for(;p;p>>=1, x=x*x) if(p&1) res=res*x; return res; }
inline int Cipolla(int n)
{
if(!n) return 0;
int a=0;
while(Check((1ll*a*a%Mod-n+Mod)%Mod)) a=Rand();
I2=(1ll*a*a%Mod-n+Mod)%Mod;
int X1=KSC((Node){a,1},(Mod+1)/2).x;
return min(X1,Mod-X1);
}
void Getsqrt(int n,vector<int> &F,vector<int> &G)
{
if(n==1) { F[0]=Cipolla(G[0]); return; }
Getsqrt((n+1)/2,F,G);
vector<int> A,B;
A.resize(n), B.resize(n);
GetInv(n,A,F);
for(ri int i=0;i<n;i++) B[i]=G[i];
NTT(n,n,A,B);
for(ri int i=0,inv2=(Mod+1)/2;i<n;i++) F[i]=1ll*(F[i]+A[i])*inv2%Mod;
}
signed main()
{
srand(time(NULL));
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();
Getsqrt(n,F,a);
for(ri int i=0;i<n;i++) printf("%d ",F[i]);
puts("");
return 0;
}
夜畔流离回,暗叹永无殿。
独隐万花翠,空寂亦难迁。
千秋孰能为,明灭常久见。
但得心未碎,踏遍九重天。