【51nod 1538】一道难题(常系数齐次线性递推)

传送门

观察式子的组合意义
就是随便填出一个序列
每次随便在 a a a里选一个填
要求和为 m m m的方案数

那么显然有转移
f i = ∑ j f i − a j f_i=\sum_jf_{i-a_j} fi=jfiaj

然后做常系数齐次线性递推即可

预处理 f 1 − 23333 f_{1-23333} f123333可以用多项式求逆

#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=104857601;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){if(a==0&&b==0)return 0;for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
typedef vector<int> poly;
namespace Poly{
	cs int C=16,M=(1<<C)+5,G=3;
	int *w[C+1],rev[M];
	inline void init_rev(int lim){
		for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
	}
	inline void init_w(){
		for(int i=1;i<=C;i++)w[i]=new int[(1<<(i-1))|1];
		int wn=ksm(G,(mod-1)/(1<<C));w[C][0]=1;
		for(int i=1,l=1<<(C-1);i<l;i++)w[C][i]=mul(w[C][i-1],wn);
		for(int i=C-1;i;i--)
		for(int j=0,l=1<<(i-1);j<l;j++)w[i][j]=w[i+1][j<<1];
	}
	inline void ntt(int *f,int lim,int kd){
		for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
		for(int mid=1,l=1,a0,a1;mid<lim;mid<<=1,l++)
		for(int i=0;i<lim;i+=mid<<1)
		for(int j=0;j<mid;j++)
		a0=f[i+j],a1=mul(w[l][j],f[i+j+mid]),f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
		if(kd==-1){
			reverse(f+1,f+lim);
			for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
		}
	}
	inline poly operator *(poly a,poly b){
		int deg=a.size()+b.size()-1;
		if(deg<=32){
			poly c(deg,0);
			for(int i=0;i<a.size();i++)
			for(int j=0;j<b.size();j++)
			Add(c[i+j],mul(a[i],b[j]));
			return c;
		}
		int lim=1;
		while(lim<deg)lim<<=1;
		init_rev(lim);
		a.resize(lim),ntt(&a[0],lim,1);
		b.resize(lim),ntt(&b[0],lim,1);
		for(int i=0;i<lim;i++)Mul(a[i],b[i]);
		ntt(&a[0],lim,-1),a.resize(deg);
		return a;
	}
	inline poly operator -(poly a,cs poly &b){
		a.resize(max(a.size(),b.size()));
		for(int i=0;i<b.size();i++)Dec(a[i],b[i]);
		return a;
	}
	inline poly Inv(poly a,int deg){
		poly b(1,::Inv(a[0])),c;
		for(int lim=4;lim<(deg<<2);lim<<=1){
			init_rev(lim);
			c.resize(lim>>1);
			for(int i=0;i<(lim>>1);i++)c[i]=(i<a.size()?a[i]:0);
			b.resize(lim),c.resize(lim);
			ntt(&b[0],lim,1),ntt(&c[0],lim,1);
			for(int i=0;i<lim;i++)Mul(b[i],dec(2,mul(b[i],c[i])));
			ntt(&b[0],lim,-1),b.resize(lim>>1);
		}b.resize(deg);return b;
	}
	inline poly operator /(poly a,poly b){
		int n=a.size(),m=b.size();
		reverse(a.bg(),a.end()),a.resize(n-m+1);
		reverse(b.bg(),b.end()),b.resize(n-m+1);
		a=a*Inv(b,n-m+1),a.resize(n-m+1);
		reverse(a.bg(),a.end());
		return a;
	}
	inline poly operator %(poly a,poly b){
		if(a.size()<b.size())return a;
		a=a-(a/b)*b,a.resize((int)b.size()-1);
		return a;
	}
}
using namespace Poly;
cs int lim=23333;
poly mo,ff;
int n,mx,A,B,v,vt[lim+5];
ll m;
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	init_w();
	n=read(),m=readll();
	v=read(),A=read(),B=read();
	if(v<=lim)chemx(mx,v),vt[v]++;
	for(int i=2;i<=n;i++){
		v=(1ll*v*A+B)%lim+1;
		chemx(mx,v),vt[v]++;
	}
	mo.resize(mx+1),ff.resize(mx+1);
	mo[mx]=1;
	for(int i=1;i<=mx;i++)mo[mx-i]=dec(0,vt[i]),ff[i]=dec(0,vt[i]);
	ff[0]++;
	ff=Inv(ff,mx+1);
	poly f(1,1),g(2);g[1]=1;
	for(;m;m>>=1,g=g*g%mo)if(m&1)f=f*g%mo;
	f.resize(mx);
	int res=0;
	for(int i=0;i<mx;i++)Add(res,mul(ff[i],f[i]));
	cout<<res<<'\n';
	return 0;
}

你可能感兴趣的:(多项式,常系数齐次线性递推)