【模板】BM算法

贴个板子,没有题解。

其实构造线性递推式的做法真的挺简单,真正扯淡的是证明BM求出的确实是最短线性递推式。

TMD为什么是最短,我也不会证,暂时只能感性理解。


代码:

#include<bits/stdc++.h>
#define ll long long
#define re register
#define gc get_char
#define cs const

namespace IO{
	inline char get_char(){
		static cs int Rlen=1<<22|1;
		static char buf[Rlen],*p1,*p2;
		return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
	}
	
	template<typename T>
	inline T get(){
		char c;
		while(!isdigit(c=gc()));T num=c^48;
		while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
		return num;
	}
	inline int getint(){return get<int>();}
}
using namespace IO;

using std::cerr;
using std::cout;

cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return r>=mod?r%mod:r;}
inline int power(int a,int b,int res=1){
	for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));
	return res;
}
inline void Inc(int &a,int b){(a+=b)>=mod&&(a-=mod);}
inline void Dec(int &a,int b){(a-=b)<0&&(a+=mod);}
inline void Mul(int &a,int b){a=mul(a,b);}

typedef std::vector<int> Poly;

std::ostream &operator<<(std::ostream &out,cs Poly &a){
	if(!a.size())out<<"empty ";
	for(int re i:a)out<<i<<" ";
	return out;
}

cs int bit=20,SIZE=1<<20|1;
int r[SIZE],*w[bit+1];
inline void init_NTT(){
	for(int re i=1;i<=bit;++i)w[i]=new int[1<<(i-1)];
	int wn=power(3,mod-1>>bit);
	w[bit][0]=1;for(int re i=1;i<(1<<bit-1);++i)w[bit][i]=mul(w[bit][i-1],wn);
	for(int re i=bit-1;i;--i)
	for(int re j=0;j<(1<<i-1);++j)w[i][j]=w[i+1][j<<1];
}
inline void NTT(Poly &A,int len,int typ){
	for(int re i=0;i<len;++i)if(i<r[i])std::swap(A[i],A[r[i]]);
	for(int re i=1,d=1;i<len;i<<=1,++d)
	for(int re j=0;j<len;j+=i<<1)
	for(int re k=0;k<i;++k){
		int &t1=A[j+k],&t2=A[i+j+k],t=mul(t2,w[d][k]);
		t2=dec(t1,t),Inc(t1,t);
	}
	if(typ==-1){
		std::reverse(A.begin()+1,A.begin()+len);
		for(int re i=0,inv=power(len,mod-2);i<len;++i)Mul(A[i],inv);
	}
}
inline void init_rev(int l){
	for(int re i=0;i<l;++i)r[i]=r[i>>1]>>1|((i&1)?l>>1:0);
}

inline Poly operator+(cs Poly &a,cs Poly &b){
	Poly c=a;if(b.size()>a.size())c.resize(b.size());
	for(int re i=0;i<b.size();++i)Inc(c[i],b[i]);
	return c;
}

inline Poly operator-(cs Poly &a,cs Poly &b){
	Poly c=a;if(b.size()>a.size())c.resize(b.size());
	for(int re i=0;i<b.size();++i)Dec(c[i],b[i]);
	return c;
}

inline Poly operator*(Poly a,Poly b){
	if(!a.size()||!b.size())return Poly(0,0);
	int deg=a.size()+b.size()-1,l=1;
	if(deg<128){
		Poly c(deg,0);
		for(int re i=0,li=a.size();i<li;++i)
		for(int re j=0,lj=b.size();j<lj;++j)Inc(c[i+j],mul(a[i],b[j]));
		return c;
	}
	while(l<deg)l<<=1;
	init_rev(l);
	a.resize(l),NTT(a,l,1);
	b.resize(l),NTT(b,l,1);
	for(int re i=0;i<l;++i)a[i]=mul(a[i],b[i]);
	NTT(a,l,-1);a.resize(deg);
	return a;
}

inline Poly Inv(cs Poly &a,int lim){
	Poly c,b(1,power(a[0],mod-2));
	for(int re l=4;(l>>2)<lim;l<<=1){
		c=a;c.resize(l>>1);
		init_rev(l);
		c.resize(l),NTT(c,l,1);
		b.resize(l),NTT(b,l,1);
		for(int re i=0;i<l;++i)b[i]=mul(b[i],dec(2,mul(b[i],c[i])));
		NTT(b,l,-1);b.resize(l>>1);
	}
	b.resize(lim);
	return b;
}

inline Poly operator/(Poly a,Poly b){
	if(a.size()<b.size())return Poly(0,0);
	int l=1,deg=a.size()-b.size()+1;
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	while(l<deg)l<<=1;
	b=Inv(b,l);b.resize(deg); 
	a=a*b;a.resize(deg);
	reverse(a.begin(),a.end());
	return a;
}

inline Poly operator%(cs Poly &a,cs Poly &b){
	if(a.size()<b.size())return a;
	Poly c=a-(a/b)*b;
	c.resize(b.size()-1);
	return c;
}

inline Poly Ksm(Poly a,int b,Poly mod){
	Poly res(1,1);
	while(b){
		if(b&1)res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}
namespace BM{
	cs int M=1e4+4;
	int L,cnt,a[M],fail[M],delta[M];
	Poly R[M];
	inline Poly solve(){
		for(int re i=1;i<=L;++i){
			int d=a[i];
			for(int re j=1,lj=R[cnt].size();j<lj;++j)Dec(d,mul(R[cnt][j],a[i-j]));
			if(!d)continue;
			fail[cnt]=i,delta[cnt]=d;
			if(!cnt){++cnt;R[cnt].resize(i+1);}
			else {
				int coef=mul(delta[cnt],power(delta[cnt-1],mod-2));
				R[cnt+1].resize(i-fail[cnt-1]);R[cnt+1].push_back(coef);
				for(int re j=1,lj=R[cnt-1].size();j<lj;++j)R[cnt+1].push_back(mul(mod-coef,R[cnt-1][j]));
				R[cnt+1]=R[cnt+1]+R[cnt];++cnt;
			}
		}
		return R[cnt];
	}
}

int n,m;
Poly f,g;
signed main(){
#ifdef zxyoi
	freopen("BM.in","r",stdin);
#endif
	init_NTT();
	n=getint(),m=getint();BM::L=n;
	for(int re i=1;i<=n;++i)BM::a[i]=getint();
	f=BM::solve();
	for(int re i=1,li=f.size();i<li;++i)cout<<f[i]<<" ";cout<<"\n";
	std::reverse(f.begin(),f.end());
	for(int re i=0,li=f.size();i<li;++i)f[i]=dec(0,f[i]);f.back()=1;
	g.resize(2);g[1]=1;
	g=Ksm(g,m,f);
	int ans=0;
	for(int re i=0;i<g.size();++i)Inc(ans,mul(g[i],BM::a[i+1]));
	cout<<ans<<"\n";
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值