Codechef Walk on Tree(BM+常系数齐次线性递推)

三世轮回 提交于 2019-11-26 16:59:27

传送门
这题的重点不在用BMBM求递推式,也不在求出递推式后的常系数齐次线性递推,而在下面这个结论:

对于一个矩阵AA,我们给出nn个二元组(ai,bi)(a_i,b_i)并定义一个函数f(k)=i=1nAai,bikf(k)=\sum_{i=1}^nA^k_{a_i,b_i}。 那么ff数列存在递推式,且这个递推式就是其特征多项式。 可以按照特征多项式的性质证明一下。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
const int rlen=1<<18|1;
inline char gc(){
	static char buf[rlen],*ib,*ob;
	(ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin));
	return ib==ob?-1:*ib++;
}
inline int read(){
	int ans=0;
	char ch=gc();
	while(!isdigit(ch))ch=gc();
	while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
	return ans;
}
const int mod=998244353;
typedef long long ll;
typedef vector<int> poly;
inline int add(int a,int b){return(a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return(a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return(ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:a-=mod;}
inline void Dec(int&a,int b){(a-=b)<0?a+=mod:a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
int w[23],invv[23],lim,tim;
vector<int>rev[23];
inline void init_w(){
	w[22]=ksm(3,(mod-1)>>23);
	for(ri i=21;~i;--i)w[i]=mul(w[i+1],w[i+1]);
	invv[0]=1;
	for(ri iv=mod+1>>1,i=1;i<23;++i)invv[i]=mul(invv[i-1],iv);
}
inline void init(const int&up){
	lim=1,tim=0;
	while(lim<up)lim<<=1,++tim;
	if(rev[tim].size())return;
	rev[tim].resize(lim);
	for(ri i=1;i<lim;++i)rev[tim][i]=(rev[tim][i>>1]>>1)|((i&1)<<(tim-1));
}
inline void ntt(poly&a,const int&type){
	for(ri i=0;i<lim;++i)if(i<rev[tim][i])swap(a[i],a[rev[tim][i]]);
	for(ri i=1,a0,a1,t=0;i<lim;i<<=1,++t)for(ri j=0,len=i<<1;j<lim;j+=len)
	for(ri k=0,mt=1;k<i;++k,Mul(mt,w[t]))a0=a[j+k],a1=mul(a[j+k+i],mt),a[j+k]=add(a0,a1),a[j+k+i]=dec(a0,a1);
	if(~type)return; 
	reverse(++a.begin(),a.end());
	for(ri i=0;i<lim;++i)Mul(a[i],invv[tim]);
}
inline poly operator+(poly a,poly b){
	int n=a.size(),m=b.size();
	poly c(max(n,m));
	for(ri i=0;i<n;++i)Add(c[i],a[i]);
	for(ri i=0;i<m;++i)Add(c[i],b[i]);
	return c;
}
inline poly operator-(poly a,poly b){
	int n=a.size(),m=b.size();
	poly c(max(n,m));
	for(ri i=0;i<n;++i)Add(c[i],a[i]);
	for(ri i=0;i<m;++i)Dec(c[i],b[i]);
	return c;
}
inline poly operator*(poly a,poly b){
	int n=a.size(),m=b.size(),t=n+m-1;
	if(t<=128){
		poly c(t);
		for(ri i=0;i<n;++i)for(ri j=0;j<m;++j)Add(c[i+j],mul(a[i],b[j]));
		return c;
	}
	init(t);
	a.resize(lim),ntt(a,1);
	b.resize(lim),ntt(b,1);
	for(ri i=0;i<lim;++i)Mul(a[i],b[i]);
	return ntt(a,-1),a.resize(t),a;
}
inline poly poly_inv(poly a,int k){
	poly b(1,ksm(a[0],mod-2)),c;
	for(ri i=4,up=k<<2;i<up;i<<=1){
		c=a,c.resize(i>>1);
		init(i);
		b.resize(lim),ntt(b,1);
		c.resize(lim),ntt(c,1);
		for(ri j=0;j<lim;++j)Mul(b[j],dec(2,mul(b[j],c[j])));
		ntt(b,-1),b.resize(i>>1);
	}
	return b.resize(k),b;
}
inline poly operator/(poly a,poly b){
	int n=a.size(),m=b.size(),t=n-m+1;
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end()),b=poly_inv(b,t);
	return a=a*b,a.resize(t),reverse(a.begin(),a.end()),a;
}
inline poly operator%(poly a,poly b){if(a.size()<b.size())return a;poly ret=a-a/b*b;return ret.resize(b.size()),ret;}
const int N=6050;
namespace Cayley_Hamilton{
	int a[N],k;
	poly md,f,g;
	inline void init(poly coe,int*A,int k_){
		k=k_;
		for(ri i=0;i<k;++i)a[i]=A[i+1];
		md.resize(k+1);
		md[k]=1;
		for(ri i=1;i<=k;++i)md[k-i]=coe[i]?mod-coe[i]:0;
	}
	inline poly query(int n){
		f.resize(2,0),g.resize(1,0);
		f[1]=g[0]=1;
		for(;n;n>>=1,f=f*f%md)if(n&1)g=g*f%md;
		return g;
	}
}
int n,rt,K,f[N][N];
vector<int>e[N];
namespace Berlekamp_Massey{
	int a[N],cnt,fail[N],det[N];
	poly R[N];
	inline void update(int n){
		int t=a[n];
		for(ri i=1,up=R[cnt].size();i<up;++i)Dec(t,mul(R[cnt][i],a[n-i]));
		if(!t)return;
		det[fail[cnt]=n]=t;
		if(!cnt){R[++cnt].resize(n+1);return;}
		Mul(t,ksm(det[fail[cnt-1]],mod-2));
		R[cnt+1].resize(n-fail[cnt-1]),R[cnt+1].push_back(t);
		t=mod-t;
		for(ri i=1,up=R[cnt-1].size();i<up;++i)R[cnt+1].push_back(mul(t,R[cnt-1][i]));
		R[cnt+1]=R[cnt+1]+R[cnt],++cnt;
	}
	inline void build(int n,int*A){
		for(ri i=1;i<=n;++i)a[i]=A[i-1],update(i);
		Cayley_Hamilton::init(R[cnt],A,R[cnt].size()-1);
	}
}
int main(){
	n=read();
	init_w();
	for(ri i=1,u,v;i<n;++i){
		u=read(),v=read();
		e[u].push_back(v);
		e[v].push_back(u);
	}
	rt=read();
	f[rt][0]=1;
	int tim=n*2+15,K=read();
	for(ri T=1;T<=tim;++T)for(ri i=1;i<=n;++i)for(ri j=0;j<e[i].size();++j)Add(f[i][T],f[e[i][j]][T-1]);
	if(K<=tim){
		for(ri i=1;i<=n;++i)cout<<f[i][K]<<' ';
		puts("");
		return 0;
	}
	Berlekamp_Massey::build(tim,f[rt]);
	poly g=Cayley_Hamilton::query(K);
	for(ri i=1,ss=0,up=Cayley_Hamilton::k;i<=n;++i,ss=0){
		for(ri j=0;j<up;++j)Add(ss,mul(f[i][j],g[j]));
		cout<<ss<<' ';
	}
	return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!