QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#140282 | #4605. 生成树计数 | myee | 100 ✓ | 281ms | 10864kb | C++11 | 29.3kb | 2023-08-15 16:54:41 | 2023-08-15 16:54:42 |
Judging History
answer
#include <algorithm>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
T ans=1%mod;
while(index)
{
if(index&1)ans=ans*base%mod;
base=base*base%mod,index>>=1;
}
return ans;
}
namespace ConstMod
{
template<const ullt p>
class mod_ullt
{
private:
ullt v;
inline ullt chg(ullt w){return(w<p)?w:w-p;}
inline mod_ullt _chg(ullt w){mod_ullt ans;ans.v=(w<p)?w:w-p;return ans;}
public:
mod_ullt():v(0){}
mod_ullt(ullt v):v(v%p){}
bol empty(){return!v;}
inline ullt val(){return v;}
friend bol operator<(mod_ullt a,mod_ullt b){return a.v<b.v;}
friend bol operator>(mod_ullt a,mod_ullt b){return a.v>b.v;}
friend bol operator<=(mod_ullt a,mod_ullt b){return a.v<=b.v;}
friend bol operator>=(mod_ullt a,mod_ullt b){return a.v>=b.v;}
friend bol operator==(mod_ullt a,mod_ullt b){return a.v==b.v;}
friend bol operator!=(mod_ullt a,mod_ullt b){return a.v!=b.v;}
inline friend mod_ullt operator+(mod_ullt a,mod_ullt b){return a._chg(a.v+b.v);}
inline friend mod_ullt operator-(mod_ullt a,mod_ullt b){return a._chg(a.v+a.chg(p-b.v));}
inline friend mod_ullt operator*(mod_ullt a,mod_ullt b){return a.v*b.v;}
friend mod_ullt operator/(mod_ullt a,mod_ullt b){return b._power(p-2)*a.v;}
friend mod_ullt operator^(mod_ullt a,ullt b){return a._power(b);}
inline mod_ullt operator-(){return _chg(p-v);}
mod_ullt sqrt()
{
if(power(v,(p-1)>>1,p)!=1)return 0;
mod_ullt b=1;do b++;while(b._power((p-1)>>1)==1);
ullt t=p-1,s=0,k=1;while(!(t&1))s++,t>>=1;
mod_ullt x=_power((t+1)>>1),e=_power(t);
while(k<s)
{
if(e._power(1llu<<(s-k-1))!=1)x*=b._power((1llu<<(k-1))*t);
e=_power(p-2)*x*x,k++;
}
return _min(x,-x),x;
}
mod_ullt inv(){return _power(p-2);}
mod_ullt _power(ullt index){mod_ullt ans(1),w(v);while(index){if(index&1)ans*=w;w*=w,index>>=1;}return ans;}
voi read(){v=0;chr c;do c=getchar();while(c>'9'||c<'0');do v=(c-'0'+v*10)%p,c=getchar();while(c>='0'&&c<='9');v%=p;}
voi print()
{
static chr C[20];uint tp=0;
ullt w=v;do C[tp++]=w%10+'0',w/=10;while(w);
while(tp--)putchar(C[tp]);
}
voi println(){print(),putchar('\n');}
mod_ullt operator++(int){mod_ullt ans=*this;return v=chg(v+1),ans;}
public:
inline ullt&operator()(){return v;}
inline mod_ullt&operator+=(mod_ullt b){return*this=_chg(v+b.v);}
inline mod_ullt&operator-=(mod_ullt b){return*this=_chg(v+chg(p-b.v));}
inline mod_ullt&operator*=(mod_ullt b){return*this=v*b.v;}
mod_ullt&operator^=(ullt b){return*this=_power(b);}
mod_ullt&operator/=(mod_ullt b){return*this=b._power(p-2)*v;}
mod_ullt&operator++(){return v=chg(v+1),*this;}
};
}
namespace NTT_POLY
{
template<const ullt p,const ullt g>
class poly_NTT
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
private:
modvec V;
public:
class NTT
{
private:
uint n;uint*T;modint*G;
public:
NTT():n(0),T(NULL),G(NULL){}
NTT(uint len)
{
n=1;while(n<len)n<<=1;
T=new uint[n],G=new modint[n],T[0]=0,G[0]=1;
for(uint i=1;i<n;i++)T[i]=((i&1)?n|T[i>>1]:T[i>>1])>>1;
modint w=power(g,(p-1)/n,p),*End=G+n;
for(modint*_G=G+1;_G<End;_G++)*_G=_G[-1]*w;
}
~NTT(){if(T!=NULL)delete[]T,delete[]G,T=NULL,G=NULL;}
inline uint size(){return n;}
voi bzr(uint len)
{
n=1;while(n<len)n<<=1;
if(T!=NULL)delete[]T,delete[]G;
T=new uint[n],G=new modint[n],T[0]=0,G[0]=1;
for(uint i=1;i<n;i++)T[i]=((i&1)?n|T[i>>1]:T[i>>1])>>1;
modint w=power(g,(p-1)/n,p),*End=G+n;
for(modint*_G=G+1;_G<End;_G++)*_G=_G[-1]*w;
}
voi ntt(modvec&x,bol op)
{
if(x.size()<n)x.resize(n);
for(uint i=0;i<n;i++)if(T[i]<i)std::swap(x[i],x[T[i]]);
for(uint i=1;i<n;i<<=1)for(uint j=0;j<n;j+=i<<1)
{
modint*w=G;
for(uint k=0;k<i;k++,w+=n/(2*i))
{
modint t=x[i+j+k]*(*w);
x[i+j+k]=x[j+k]-t,x[j+k]+=t;
}
}
if(op)
{
for(uint i=1;i*2<n;i++)std::swap(x[i],x[n-i]);
modint t=modint(n).inv();for(uint i=0;i<n;i++)x[i]*=t;
}
}
inline modint Omega(uint n){return G[n%size()];}
NTT&operator=(NTT b)
{
if(T!=NULL)delete[]T,delete[]G,T=NULL,G=NULL;
if(b.T==NULL)return*this;
T=new uint[n],G=new modint[n=b.n];
for(uint i=0;i<n;i++)T[i]=b.T[i],G[i]=b.G[i];
return*this;
}
};
class DIFDIT
{
private:
uint n;modint*G;
public:
DIFDIT():n(0),G(NULL){}
DIFDIT(uint len)
{
n=1;while(n<len)n<<=1;
G=new modint[n],G[0]=1;
modint w=power(g,(p-1)/n,p),*End=G+n;
for(modint*_G=G+1;_G<End;_G++)*_G=_G[-1]*w;
}
~DIFDIT(){if(G!=NULL)delete[]G,G=NULL;}
inline uint size(){return n;}
voi bzr(uint len)
{
n=1;while(n<len)n<<=1;
if(G!=NULL)delete[]G;
G=new modint[n],G[0]=1;
modint w=power(g,(p-1)/n,p),*End=G+n;
for(modint*_G=G+1;_G<End;_G++)*_G=_G[-1]*w;
}
voi dif(modvec&x)
{
if(x.size()<n)x.resize(n);
for(uint i=n>>1;i;i>>=1)for(uint j=0;j<n;j+=i<<1)
{
modint*w=G;
for(uint k=0;k<i;k++,w+=n/(2*i))
{
modint u=x[j+k],t=x[i+j+k];
x[j+k]=u+t,x[i+j+k]=(u-t)*(*w);
}
}
}
voi dit(modvec&x)
{
if(x.size()<n)x.resize(n);
for(uint i=1;i<n;i<<=1)for(uint j=0;j<n;j+=i<<1)
{
modint*w=G;
for(uint k=0;k<i;k++,w+=n/(2*i))
{
modint t=x[i+j+k]*(*w);
x[i+j+k]=x[j+k]-t,x[j+k]+=t;
}
}
for(uint i=1;i*2<n;i++)std::swap(x[i],x[n-i]);
modint t=modint(n).inv();for(uint i=0;i<n;i++)x[i]*=t;
}
DIFDIT&operator=(DIFDIT b)
{
if(G!=NULL)delete[]G,G=NULL;
if(b.G==NULL)return*this;
G=new modint[n=b.n];
for(uint i=0;i<n;i++)G[i]=b.G[i];
return*this;
}
};
public:
poly_NTT(){}
poly_NTT(uint n){V.resize(n);}
poly_NTT(modvec V):V(V){}
inline voi bzr(){V.clear();}
inline voi push(modint v){V.push_back(v);}
inline voi pop(){V.pop_back();}
inline voi cut_zero(){while(!V.empty()&&V.back().empty())V.pop_back();}
inline voi chg_siz(uint n){V.resize(n);}
inline voi chg_deg(uint n){V.resize(n+1);}
inline bol empty(){return cut_zero(),V.empty();}
inline uint size(){return V.size();}
inline uint deg(){return V.size()-1;}
inline modint val(uint n){return(n<size())?V[n]:0;}
inline modvec GET(){return V;}
poly_NTT reverse()
{
poly_NTT ans;for(uint i=size()-1;~i;i--)ans.push(V[i]);
return ans;
}
friend poly_NTT operator+(poly_NTT a,poly_NTT b)
{
if(a.size()<b.size())a.chg_siz(b.size());
for(uint i=0;i<b.size();i++)a[i]+=b[i];
a.cut_zero();return a;
}
friend poly_NTT operator+(poly_NTT a,modint v)
{
if(!a.size())a.chg_siz(1);
a[0]+=v;return a;
}
friend poly_NTT operator+(modint v,poly_NTT a)
{
if(!a.size())a.chg_siz(1);
a[0]+=v;return a;
}
friend poly_NTT operator-(poly_NTT a){return a*modint(p-1);}
friend poly_NTT operator-(poly_NTT a,poly_NTT b)
{
if(a.size()<b.size())a.chg_siz(b.size());
for(uint i=0;i<b.size();i++)a[i]-=b[i];
a.cut_zero();return a;
}
friend poly_NTT operator-(poly_NTT a,modint v)
{
if(!a.size())a.chg_siz(1);
a[0]-=v;return a;
}
friend poly_NTT operator-(modint v,poly_NTT a)
{
if(!a.size())a.chg_siz(1);
a[0]-=v;return-a;
}
friend poly_NTT operator*(poly_NTT a,poly_NTT b)
{
modvec&A=a.V,&B=b.V;DIFDIT s(A.size()+B.size());
s.dif(A),s.dif(B);for(uint i=0;i<s.size();i++)A[i]*=B[i];
s.dit(A),a.cut_zero();return a;
}
friend poly_NTT operator*(poly_NTT A,modint b)
{
for(auto&s:A.V)s*=b;
return A;
}
friend poly_NTT operator*(modint b,poly_NTT A)
{
for(auto&s:A.V)s*=b;
return A;
}
friend poly_NTT operator<<(poly_NTT a,uint k)
{
poly_NTT ans;ans.chg_siz(k);for(auto v:a.V)ans.push(v);
return ans;
}
friend poly_NTT operator>>(poly_NTT a,uint k)
{
poly_NTT ans;for(uint i=k;i<a.size();i++)ans.push(a[i]);
return ans;
}
friend poly_NTT sub_mul(poly_NTT a,poly_NTT b)
{
uint len=(a=a.reverse()).size();
modvec&A=a.V,&B=b.V;
DIFDIT s(len+B.size());
s.dif(A),s.dif(B);for(uint i=0;i<s.size();i++)A[i]*=B[i];
s.dit(A),a.chg_siz(len),a=a.reverse();return a;
}
poly_NTT inv(){return inv(size());}
poly_NTT inv(uint prec)
{
modvec ans;DIFDIT s;ans.push_back(V[0].inv());
for(uint tp=1;tp<prec;tp<<=1)
{
modvec f(tp<<1),t=ans;
for(uint i=0;i<(tp<<1);++i)f[i]=val(i);
s.bzr(tp<<1),s.dif(f),s.dif(t);
for(uint i=0;i<(tp<<1);++i)f[i]=1-f[i]*t[i];
s.dit(f);
for(uint i=0;i<tp;++i)f[i]=f[i+tp],f[i+tp]=0;
s.dif(f);
for(uint i=(tp<<1)-1;~i;--i)f[i]*=t[i];
s.dit(f),ans.resize(tp<<1);
for(uint i=0;i<tp;++i)ans[i+tp]=f[i];
}
ans.resize(prec);return ans;
}
poly_NTT sqrt(){return sqrt(size());}
poly_NTT sqrt(uint prec)
{
modvec ans,Inv;ans.push_back(val(0).sqrt()),Inv.push_back(ans[0].inv());
DIFDIT s;
modint w=modint(2).inv();
for(uint tp=1;tp<prec;tp<<=1)
{
s.bzr(tp<<2);
modvec f(tp<<1);for(uint i=0;i<(tp<<1);i++)f[i]=val(i);
s.dif(Inv);s.dif(ans);
for(uint i=0;i<(tp<<2);i++)Inv[i]*=2-Inv[i]*ans[i];
s.dit(Inv),Inv.resize(tp);s.dif(Inv);
for(uint i=0;i<(tp<<2);i++)Inv[i]*=2-Inv[i]*ans[i];
s.dit(Inv),Inv.resize(tp<<1);
modvec user=Inv;s.dif(Inv),s.dif(f);
for(uint i=0;i<(tp<<2);i++)ans[i]=(ans[i]+Inv[i]*f[i])*w;
s.dit(ans),Inv=user,ans.resize(tp<<1);
}
ans.resize(prec);return ans;
}
poly_NTT diff()
{
poly_NTT ans;for(uint i=1;i<size();i++)ans.push(i*V[i]);
return ans;
}
poly_NTT inte()
{
if(!size())return*this;
poly_NTT ans(size()+1);ans[1]=1;
for(uint i=2;i<=size();i++)ans[i]=-ans[p%i]*(p/i);
for(uint i=1;i<=size();i++)ans[i]*=V[i-1];
return ans;
}
poly_NTT ln(){return ln(size());}
poly_NTT ln(uint prec)
{
poly_NTT a=this->diff()*this->inv(prec);a.chg_siz(prec),a=a.inte(),a.chg_siz(prec);return a;
}
poly_NTT exp(){return exp(size());}
poly_NTT exp(uint prec)
{
poly_NTT ans;modvec Inv;ans.push(1),Inv.push_back(1);
for(uint tp=1;tp<prec;tp<<=1)
{
modvec f,ff=ans.diff().V;
for(uint i=0;i<(tp<<1);i++)f.push_back(val(i));
f[0]=1;DIFDIT s(tp<<2);s.dif(ans.V),s.dif(Inv);
for(uint i=0;i<(tp<<2);i++)Inv[i]*=2-Inv[i]*ans[i];
s.dit(Inv),Inv.resize(tp);s.dif(Inv);
for(uint i=0;i<(tp<<2);i++)Inv[i]*=2-Inv[i]*ans[i];
s.dit(Inv),Inv.resize(tp<<1);s.dif(Inv);s.dif(ff);
for(uint i=0;i<(tp<<2);i++)ff[i]*=Inv[i];
s.dit(ff);f=(f-poly_NTT(ff).inte()).V;s.dif(f);
for(uint i=0;i<(tp<<2);i++)ans[i]*=f[i];
s.dit(Inv),s.dit(ans.V),ans.chg_siz(tp<<1);
}
ans.chg_siz(prec);return ans;
}
friend poly_NTT operator/(poly_NTT a,poly_NTT b)
{
a.cut_zero(),b.cut_zero();if(a.size()<b.size())return poly_NTT();
poly_NTT ans=a.reverse()*b.reverse().inv(a.size()-b.size()+1);
ans.chg_siz(a.size()-b.size()+1);return ans.reverse();
}
friend poly_NTT operator%(poly_NTT a,poly_NTT b){return a-a/b*b;}
public:
inline modint&operator[](uint n){return V[n];};
poly_NTT&operator+=(poly_NTT b)
{
if(size()<b.size())chg_siz(b.size());
for(uint i=0;i<b.size();i++)V[i]+=b[i];
cut_zero();return*this;
}
inline poly_NTT&operator+=(modint v)
{
if(!size())chg_siz(1);
V[0]+=v;return*this;
}
poly_NTT&operator-=(poly_NTT b)
{
if(size()<b.size())chg_siz(b.size());
for(uint i=0;i<b.size();i++)V[i]-=b[i];
cut_zero();return*this;
}
inline poly_NTT&operator-=(modint v)
{
if(!size())chg_siz(1);
V[0]-=v;return*this;
}
poly_NTT&operator*=(poly_NTT b)
{
modvec&A=V,&B=b.V;
DIFDIT s(A.size()+B.size());
s.dif(A),s.dif(B);
for(uint i=0;i<s.size();i++)A[i]*=B[i];
s.dit(A),cut_zero();return*this;
}
poly_NTT&operator*=(modint v)
{
for(auto&t:V)t*=v;
return*this;
}
poly_NTT&operator/=(poly_NTT b){return*this=*this/b;}
poly_NTT&operator%=(poly_NTT b){return*this=*this%b;}
poly_NTT&operator<<=(uint v){return*this=*this<<v;}
poly_NTT&operator>>=(uint v){return*this=*this>>v;}
};
template<const ullt p,const ullt g>
class poly_eval
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
typedef poly_NTT<p,g>poly;
private:
std::vector<poly>G,User;modvec X;
voi mult_eval_dfs1(uint u,uint l,uint r)
{
if(l+1==r){G[u].push(1),G[u].push(-X[l]);return;}
uint mid=(l+r)/2;mult_eval_dfs1(u<<1,l,mid),mult_eval_dfs1(u<<1|1,mid,r),G[u]=G[u<<1]*G[u<<1|1];
}
voi mult_eval_dfs2(uint u,uint l,uint r)
{
User.back().chg_siz(r-l);
if(l+1==r){X[l]=User.back().val(0);return;}
uint mid=(l+r)/2;
User.push_back(sub_mul(User.back(),G[u<<1|1])),mult_eval_dfs2(u<<1,l,mid);
User.back()=sub_mul(User[User.size()-2],G[u<<1]),mult_eval_dfs2(u<<1|1,mid,r);
User.pop_back();
}
public:
voi operator()(poly P,modvec&X)
{
if(X.empty())return;
G.resize(X.size()<<2),User.clear(),this->X=X;
mult_eval_dfs1(1,0,X.size());
User.push_back(sub_mul(P,G[1].inv(std::max<uint>(P.size(),X.size())+1)));
mult_eval_dfs2(1,0,X.size());
G.clear(),User.clear(),X=this->X,this->X.clear();
}
};
template<const ullt p,const ullt g>
class poly_inter
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
typedef poly_NTT<p,g>poly;
typedef poly_eval<p,g>eval;
private:
std::vector<poly>Lim,F,G;modvec X,H;
voi dfs(uint l,uint r)
{
if(l+1==r)
{
F.push_back(poly()),F.back().push(H[l]),G.push_back(poly()),G.back().push(-X[l]),G.back().push(1);return;
}
uint mid=(l+r)>>1;dfs(l,mid),dfs(mid,r);
F[F.size()-2]=F[F.size()-2]*G.back()+F.back()*G[G.size()-2],F.pop_back(),G[G.size()-2]*=G.back(),G.pop_back();
}
public:
poly operator()(modvec X,modvec Y)
{
uint n=std::min(X.size(),Y.size());if(!n)return poly();
X.resize(n),Y.resize(n),this->X=X;poly P;Lim.clear();
for(uint i=0;i<n;i++)
{
P.bzr(),P.push(-X[i]),P.push(1);
uint w=lowbit(i+1);while(w>>=1)P*=Lim.back(),Lim.pop_back();
Lim.push_back(P);
}
P=Lim.back(),Lim.pop_back();while(Lim.size())P*=Lim.back(),Lim.pop_back();
eval()(P.diff(),X),H.resize(n);for(uint i=0;i<n;i++)H[i]=Y[i]/X[i];
F.clear(),G.clear(),dfs(0,n);
poly ans=F.back();F.clear(),G.clear(),this->X.clear(),H.clear();return ans;
}
};
template<const ullt p,const ullt g>
class poly_cpd
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
typedef poly_NTT<p,g>poly;
modvec Turn(std::vector<llt>QAQ)
{
modvec ans;
for(uint i=0;i<QAQ.size();i++)ans.push_back((QAQ[i]%(llt)p+p)%p);
return ans;
}
modint point_eval(poly P,modint x)
{
modint ans;
for(uint i=P.deg();~i;i--)ans=ans*x+P[i];
return ans;
}
poly z_npow(poly P,uint n)
{
if(P.empty())return P;
poly ans(P.deg()*n+1);
for(uint i=0;i<P.size();i++)ans[i*n]+=P[i];
return ans;
}
poly z_npow(poly P,uint n,uint prec)
{
poly ans(prec);
for(uint i=0;i<P.size()&&i*n<prec;i++)ans[i*n]+=P[i];
return ans;
}
poly z_mul_k(poly P,modint k)
{
modint t(1);
for(uint i=0;i<P.size();i++)P[i]*=t,t*=k;
return P;
}
poly z_add_v(poly P,modint v)
{
uint n=P.size();if(!n)return P;
modvec A(n),B(n);
A[0]=1;for(uint i=1;i<n;i++)A[i]=A[i-1]*i;
B[n-1]=A[n-1].inv();for(uint i=n-1;i;i--)B[i-1]=B[i]*i;
poly User(n);modint w(1);
for(uint i=0;i<n;i++)P[i]*=A[i],User[i]=w*B[i],w*=v;
P=sub_mul(P,User),P.chg_siz(n);
for(uint i=0;i<n;i++)P[i]*=B[i];
return P;
}
poly chg_siz(poly P,uint siz){P.chg_siz(siz);return P;}
poly PolyaInv(poly P,uint prec){return(modint(1)-P).inv(prec);}
poly PolyaExp(poly P,uint prec)
{
modvec inv(prec);
inv[1]=1;for(uint i=2;i<prec;i++)inv[i]=(p/i)*-inv[p%i];
poly ans(prec);
for(uint i=1;i<prec;i++)for(uint j=1;i*j<prec&&j<P.size();j++)ans[i*j]+=P[j]*inv[i];
return ans.exp(prec);
}
poly PolyaInv(poly P){return PolyaInv(P,P.size());}
poly PolyaExp(poly P){return PolyaExp(P,P.size());}
voi println(poly P,uint n)
{
for(uint i=0;i<n;i++){
if(i)putchar(' ');
P.val(i).print();
}
putchar('\n');
}
voi println(poly P){println(P,P.size());}
};
template<const ullt p,const ullt g>
class poly_nums
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
typedef poly_NTT<p,g>poly;
typedef poly_cpd<p,g>cpd;
modvec PowSum(uint n,uint m)
{
modvec P(n+2),Q(n+2);
P[0]=1;for(uint i=1;i<=n+1;i++)P[i]=P[i-1]*i;
Q[n+1]=P[n+1].inv();for(uint i=n+1;i;i--)Q[i-1]=Q[i]*i;
poly A(n+1);for(uint i=0;i<=n;i++)A[i]=Q[i+1];
A=A.inv();
poly B(n+1);modint v(1);for(uint i=0;i<=n;i++)B[i]=v*Q[i],v*=m;
B=(B*A-A)>>1;B.chg_siz(n);
for(uint i=0;i<n;i++)B[i]*=P[i];
return B.GET();
}
modvec S1R(uint n)
{
if(!n)return modvec({modint(1)});
if(n&1)return(S1R(n-1)*poly(modvec({n-1,1}))).GET();
poly P=S1R(n>>1);P*=cpd().z_add_v(P,n>>1);return P.GET();
}
modvec S1C(uint n,uint prec)
{
if(n>=prec)return modvec(prec);
modvec P(prec+1),Q(prec+1);
P[0]=1;for(uint i=1;i<=prec;i++)P[i]=P[i-1]*i;
Q[prec]=P[prec].inv();for(uint i=prec;i;i--)Q[i-1]=Q[i]*i;
poly ans;
for(uint i=1;i<=prec-n;i++)ans.push(Q[i]*P[i-1]);
ans=(ans.ln(prec-n)*modint(n)).exp(prec-n)<<n;
modint v=1;
for(uint i=1;i<=n;i++)v*=i;
ans=ans*v.inv();
for(uint i=n;i<prec;i++)ans[i]*=P[i];
return ans.GET();
}
modvec S2R(uint n)
{
modvec P(n+1),Q(n+1);
P[0]=1;for(uint i=1;i<=n;i++)P[i]=P[i-1]*i;
Q[n]=P[n].inv();for(uint i=n;i;i--)Q[i-1]=Q[i]*i;
poly A(n+1),B(n+1);
A[0]=!n;if(n)A[1]=1;
std::vector<uint>Prime;
std::vector<bol>Gone(n+1);
for(uint i=2;i<=n;i++)
{
if(!Gone[i]){Prime.push_back(i);modint v=modint(i)._power(n);for(ullt j=i;j<=n;j*=i)A[j]=v*A[j/i],Gone[j]=true;}
for(auto w:Prime)if(i*w<=n&&i%w){for(ullt j=w;i*j<=n;j*=w)A[i*j]=A[i]*A[j],Gone[i*j]=true;}else break;
}
for(uint i=0;i<=n;i++)A[i]*=Q[i],B[i]=(i&1?p-1:1)*Q[i];
A*=B,A.chg_deg(n);
return A.GET();
}
modvec S2C(uint n,uint prec)
{
if(n>=prec)return modvec(prec);
modvec P(prec+1),Q(prec+1);
P[0]=1;for(uint i=1;i<=prec;i++)P[i]=P[i-1]*i;
Q[prec]=P[prec].inv();for(uint i=prec;i;i--)Q[i-1]=Q[i]*i;
poly ans=PowSum(prec-n,n+1);
ans[0]=0;for(uint i=1;i<prec-n;i++)ans[i]*=Q[i]*P[i-1];
ans=ans.exp(prec-n)<<n;
return ans.GET();
}
};
}
namespace FWT_MODINT
{
template<const ullt p>
class FWT_Mod
{
public:
typedef ConstMod::mod_ullt<p>modint;
typedef std::vector<modint>modvec;
private:
uint n;
public:
FWT_Mod():n(0){}
voi bzr(uint len){n=1;while(n<len)n<<=1;}
uint size(){return n;}
voi OR(modvec&x,bol op)
{
if(x.size()<n)x.resize(n);
for(uint i=1;i<n;i<<=1)for(uint j=0;j<n;j+=i<<1)for(uint k=0;k<i;k++)
op?x[i+j+k]-=x[j+k]:x[i+j+k]+=x[j+k];
}
voi AND(modvec&x,bol op)
{
if(x.size()<n)x.resize(n);
for(uint i=1;i<n;i<<=1)for(uint j=0;j<n;j+=i<<1)for(uint k=0;k<i;k++)
op?x[j+k]-=x[i+j+k]:x[j+k]+=x[i+j+k];
}
voi XOR(modvec&x,bol op)
{
if(x.size()<n)x.resize(n);
for(uint i=1;i<n;i<<=1)for(uint j=0;j<n;j+=i<<1)for(uint k=0;k<i;k++)
{
modint u=x[j+k],t=x[i+j+k];x[j+k]=u+t,x[i+j+k]=u-t;
}
if(op){modint v=modint(n).inv();for(uint i=0;i<n;i++)x[i]*=v;}
}
};
}
const ullt Mod=998244353,g=3;
typedef ConstMod::mod_ullt<Mod>modint;
typedef std::vector<modint>modvec;
typedef NTT_POLY::poly_NTT<Mod,g>poly;
typedef NTT_POLY::poly_eval<Mod,g>eval;
typedef NTT_POLY::poly_inter<Mod,g>inter;
typedef NTT_POLY::poly_cpd<Mod,g>cpd;
typedef NTT_POLY::poly_nums<Mod,g>nums;
typedef FWT_MODINT::FWT_Mod<Mod>FWT;
modint A[40005];
typedef std::pair<poly,poly>Pair;
Pair get(uint l,uint r){
if(l>=r)return{poly(),modvec{1}};
if(r-l==1)return{modvec{1},poly({1,-A[l]})};
Pair p1=get(l,(l+r)>>1),p2=get((l+r)>>1,r);
return Pair(p1.first*p2.second+p1.second*p2.first,p1.second*p2.second);
}
modvec ask(uint n,uint m){Pair p=get(0,n);return cpd().chg_siz(p.first*p.second.inv(m),m).GET();}
poly kz_Sum(poly P,uint n)
{
uint m=P.size();modvec V=ask(n,m);
for(uint i=0;i<m;i++)P[i]*=V[i];
return P;
}
modint P[100005],Q[100005];
int main()
{
#ifdef MYEE
freopen("QAQ.in","r",stdin);
freopen("QAQ.out","w",stdout);
#endif
P[0]=1;for(uint i=1;i<=100000;i++)P[i]=P[i-1]*i;
Q[100000]=P[100000].inv();for(uint i=100000;i;i--)Q[i-1]=Q[i]*i;
uint n,m;scanf("%u%u",&n,&m);for(uint i=0;i<n;i++)A[i].read();
if(n==1)return puts(m?"0":"1"),0;
poly F(n),G(n);for(uint i=0;i<n;i++)F[i]=(modint(i+1)^m)*Q[i],G[i]=(modint(i+1)^(2*m))*Q[i];
modint v=(kz_Sum(cpd().chg_siz(G*F.inv(n),n),n)*kz_Sum(F.ln(),n).exp()).val(n-2)*P[n-2];
for(uint i=0;i<n;i++)v*=A[i];
v.println();
return 0;
}
詳細信息
Test #1:
score: 5
Accepted
time: 1ms
memory: 4940kb
input:
105 14 138447198 654870701 150462538 749539275 29291082 757937945 39017800 516237051 770048433 208371121 183485972 565638440 892667426 429830095 20420107 149308492 634292609 342992316 892767631 233604646 172597402 779769499 264531516 831659743 713722094 913085287 579306192 484271595 775184458 338222...
output:
745603313
result:
ok 1 number(s): "745603313"
Test #2:
score: 5
Accepted
time: 4ms
memory: 4848kb
input:
189 23 641972211 979684612 152744946 783615120 978713222 168650336 857141516 616857395 540616523 113005973 711704024 687584327 301312674 275198920 51589456 80314015 157861752 762127499 92185899 106425187 268377739 537927189 848269317 38081352 129966693 24959563 806662836 815390176 283101118 70529095...
output:
31694519
result:
ok 1 number(s): "31694519"
Test #3:
score: 5
Accepted
time: 4ms
memory: 5056kb
input:
255 30 644732757 539051803 11521854 33672393 724898333 57201047 572157108 376478236 179179709 65318497 294731323 311061838 975241245 480268333 677060569 117849971 342083580 315785155 868382819 200079270 59824554 687361561 349882593 837076985 151361359 231945651 103914661 768822624 51062072 362357598...
output:
198283395
result:
ok 1 number(s): "198283395"
Test #4:
score: 5
Accepted
time: 6ms
memory: 5096kb
input:
479 21 425726002 487303905 37293431 95501469 427290832 900428476 754996703 736133996 43934815 494960949 987312413 635333947 129261282 468062813 784287979 243149526 861636447 278860015 226944388 909945345 495516775 65698638 337671210 862178697 622195526 997259676 284030292 21590370 502934410 81491610...
output:
332416726
result:
ok 1 number(s): "332416726"
Test #5:
score: 5
Accepted
time: 9ms
memory: 5072kb
input:
1001 23 43412769 328418634 172260660 515307451 265107560 252450730 354186179 30915291 670354972 243092380 821532610 887901814 265736742 515075155 107159529 951956978 476374470 778413044 522258384 147058289 564416380 105255288 103268840 275517718 878764216 619359944 558463566 852338938 439555478 4389...
output:
504242000
result:
ok 1 number(s): "504242000"
Test #6:
score: 5
Accepted
time: 14ms
memory: 5304kb
input:
1350 19 51792089 195765831 153834664 682500250 93550073 712089786 331935989 719713753 659375672 135741625 48747604 266234619 376000300 783975255 904432649 784067668 126677895 935652520 635232411 235725811 470906291 948563978 767164798 263773692 695228198 23765872 166571639 142065740 147681465 781765...
output:
21527657
result:
ok 1 number(s): "21527657"
Test #7:
score: 5
Accepted
time: 16ms
memory: 5136kb
input:
1999 14 977048642 436302631 51078567 715481869 937321294 40738049 39271403 736950737 829074708 197223160 776266475 680010881 145195765 861977709 405221131 794540650 842588046 156651101 467611893 323265287 355460693 137744170 725197657 13762224 727577079 996690158 270699851 57333509 403827104 4772745...
output:
119210730
result:
ok 1 number(s): "119210730"
Test #8:
score: 5
Accepted
time: 27ms
memory: 5500kb
input:
2509 28 693774123 174870138 336583497 720228549 699162721 573088543 491966100 633205770 781900217 284291396 889407725 103468453 967441012 45803434 289864228 603632648 121348048 825100009 833780846 415148480 454173856 391623014 357576987 258723971 820174171 245459016 184917741 69032619 139501944 5504...
output:
72861666
result:
ok 1 number(s): "72861666"
Test #9:
score: 5
Accepted
time: 51ms
memory: 6168kb
input:
5005 1 662319723 57173773 757645647 726996603 220670101 92998963 748404758 806177278 335795884 177330628 71230677 65521852 419502916 208926863 544809703 712331583 239364857 104977669 250992721 281232675 15113672 119283135 250544236 79314338 476188283 752159592 549820513 794094563 809015634 331358126...
output:
452373642
result:
ok 1 number(s): "452373642"
Test #10:
score: 5
Accepted
time: 117ms
memory: 7764kb
input:
10010 1 257336921 142539292 70533714 715092679 800022554 485944515 775769629 280877101 184644858 690111212 42846424 781413804 53942898 586475427 699985596 106017201 425551537 451899481 419432071 821680719 236400698 695020040 85130291 127141944 923119943 37707471 299559834 664382675 329282841 5533672...
output:
970412135
result:
ok 1 number(s): "970412135"
Test #11:
score: 5
Accepted
time: 48ms
memory: 6100kb
input:
5005 2 76151250 845540817 363885714 817247341 912391878 252921655 49257382 478924579 54420268 360118161 248430290 629132930 543292959 102242015 433093271 191508609 158347305 534693727 420196482 760310015 182444350 101548267 132308109 796578767 808363396 451057426 489003472 425626521 208186481 424059...
output:
374956031
result:
ok 1 number(s): "374956031"
Test #12:
score: 5
Accepted
time: 118ms
memory: 7532kb
input:
10015 2 310920628 839254433 220339414 515505380 582607535 748583798 875348092 158211247 370452787 716267850 294890331 247800791 29356266 417002500 82708702 223965410 174354176 941880171 785366764 631262 442792169 521920903 654306940 486539480 112881636 259298851 722799854 621079043 279305353 1027954...
output:
918785322
result:
ok 1 number(s): "918785322"
Test #13:
score: 5
Accepted
time: 51ms
memory: 6044kb
input:
5000 23 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 4385...
output:
253212914
result:
ok 1 number(s): "253212914"
Test #14:
score: 5
Accepted
time: 109ms
memory: 7428kb
input:
10000 27 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 2...
output:
895771012
result:
ok 1 number(s): "895771012"
Test #15:
score: 5
Accepted
time: 242ms
memory: 10524kb
input:
20000 19 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 743...
output:
91973126
result:
ok 1 number(s): "91973126"
Test #16:
score: 5
Accepted
time: 275ms
memory: 10724kb
input:
30000 29 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 2...
output:
903763257
result:
ok 1 number(s): "903763257"
Test #17:
score: 5
Accepted
time: 133ms
memory: 7420kb
input:
15000 15 432094160 826672471 66459847 496161113 970568934 570119738 319545126 698998332 132346021 4998 737952654 56041950 334984203 497869218 659008297 262495948 885560045 756305639 630873767 96889276 150483356 36151069 768118784 922961816 68580937 625900632 67294750 559220805 236631420 187431960 51...
output:
61109672
result:
ok 1 number(s): "61109672"
Test #18:
score: 5
Accepted
time: 237ms
memory: 10660kb
input:
20000 20 938923428 245728685 329575443 277635437 102904378 146372786 618943897 479294393 131191858 16295561 735444726 43609557 479766604 105216718 290827416 983641941 517126021 947178755 270386148 437534116 180103688 941061724 572552686 757491694 903454347 382785440 358780864 273564899 403004237 243...
output:
885273489
result:
ok 1 number(s): "885273489"
Test #19:
score: 5
Accepted
time: 269ms
memory: 10752kb
input:
28000 24 855300694 386133818 705146305 936773243 286991264 944467539 187184126 892681865 18123277 881026660 179932795 35128595 614747204 36334120 869838453 799505292 569255472 818530203 741767692 572775375 968515998 124926452 135739445 108940767 353549593 106384550 990557244 806049401 454171871 8988...
output:
350968823
result:
ok 1 number(s): "350968823"
Test #20:
score: 5
Accepted
time: 281ms
memory: 10864kb
input:
30000 30 9011967 960220911 49038306 30528077 693907891 571027429 630367765 77071332 555292056 687307517 23923128 375675120 236000142 327115127 76073627 783815174 93399649 337556192 996890954 941149537 776319700 593374944 252695496 167591842 274933366 272700955 827440792 548566078 619330066 66483862 ...
output:
908276353
result:
ok 1 number(s): "908276353"
Extra Test:
score: 0
Extra Test Passed