Berlekamp-Massey算法-loj#2356. 「NOI2007」 生成树计数

传送门:loj2356


题解

打表+BM找递推式( n 2 n^2 n2)+线性齐次递推式矩乘优化( n 2 log ⁡ k n^2\log k n2logk)
黑科技:Berlekamp-Massey算法


代码

#include
using namespace std;
const int N=500,mod=65521;
typedef long long ll;

ll n,K;int a[N],f[N],h[N],D;
int poly[5][233];
vector<int>init[5]; 
int sz[5]={1,3,7,17,45},lim[5]={5,11,25};

inline int ad(int x,int y){x+=y;return x>=mod?x-mod:x;}
inline int dc(int x,int y){x-=y;return x<0?x+mod:x;}

inline int fp(int x,int y)
{int re=1;for(;y;y>>=1,x=(ll)x*x%mod) if(y&1) re=(ll)re*x%mod;return re;}

namespace BM{
	#define pb push_back
	int cnt,fl[N],dlt[N];
	vector<int>R[N];
	vector<int> sol(){
	    int i,j,v,coef;
	    for(i=0;i<=cnt;++i) R[i].clear(),fl[i]=dlt[i]=0;
		cnt=0;R[0].pb(0);
		for(i=1;i<=n;++i){
	    	for(v=a[i],j=1;j<R[cnt].size();++j) 
			  v=dc(v,(ll)R[cnt][j]*a[i-j]%mod);
	    	if(!v) continue;dlt[cnt]=v;fl[cnt]=i;
	    	if(!cnt) R[++cnt].resize(i+1);
	    	else{
	    		R[++cnt].resize(1);
	    		for(j=i-1-fl[cnt-2];j>0;--j) R[cnt].pb(0);
	    		R[cnt].pb((coef=(ll)v*fp(dlt[cnt-2],mod-2)%mod));
	    		for(j=1;j<R[cnt-2].size();++j) R[cnt].pb((ll)R[cnt-2][j]*(mod-coef)%mod);
	    		R[cnt].resize(max(R[cnt].size(),R[cnt-1].size())); 
				for(j=1;j<R[cnt-1].size();++j) R[cnt][j]=ad(R[cnt][j],R[cnt-1][j]);
			}
		}
	    return R[cnt];
	}
}

vector<int>nw;

inline void mul(int *f,int *g)
{
	static int c[N];
	int i,j;
	for(i=0;i<=(D<<1);++i) c[i]=0;
	for(i=0;i<D;++i)
	 for(j=0;j<D;++j)
	  c[i+j]=ad(c[i+j],(ll)f[i]*g[j]%mod);
	for(i=(D<<1);i>=D;--i) if(c[i]){
		for(j=1;j<=D;++j)
		 c[i-j]=ad(c[i-j],(ll)c[i]*a[j]%mod);
		c[i]=0;
	}
	for(i=0;i<D;++i) f[i]=c[i];
}

int main(){
	init[0]={0,1,1,1,1,1};
	init[1]={0,1,1,3,8,21,55,144,377,987,2584,6765};
	init[2]={0,1,1,3,16,75,336,1488,6580,29085,63023,43933,20918,23015,29132,48248,31190,4445,16275,15023,62965,65462,41033,7792,9008,11460};
	init[3]={0,1,1,3,16,125,864,5635,35840,29517,48795,64376,52310,4486,28336,8758,64387,31184,24386,924,17339,37190,56290,63519,16901,29810,10951,54377,10900,7194,45196,49203,6552,6577,27252,8776,26557,8650,16129,47857,498,22234,5918,9943,33404,14096,45895,13602,21648,17129,36233,64359,25954,28647,29122,54220,55400,20284,25183,62301,14459,38513,50440,5427};
	init[4]={0,1,1,3,16,125,1296,12005,38927,26915,65410,9167,63054,58705,18773,9079,38064,46824,48121,50048,47533,30210,24390,51276,45393,357,44927,15398,15923,31582,56586,25233,41258,21255,21563,16387,39423,26418,10008,6962,42377,50881,54893,50452,23715,53140,52131,57691,13625,19479,37874,34633,61220,42575,35931,22461,32377,49296,12287,32887,44563,25055,37753,54336,11668,16467,3267,49972,21276,29700,9569,24951,56257,6220,42130,17892,44424,54362,25175,43989,58472,30146,51698,10010,61771,9533,12296,50446,62591,39451,6049,55426,23860,53435,55903,61846,26992,34882,49338,53785,33687,5239,62781,8739,54104,24441,19229,28336,57354,722,59600,33638,10906,14166,2464,40492,6525,6298,5054,65202,3061,21953,33042,62169,413,26688,41534,18905,18514,174,5800,45851,28806,51230,463,39912,14722,33942,62699,9081,721,27566,62212,46167,64399,6451,61130,31757,53456,30645,956,31691,4229,39383,30503,49884,7394,7757,5169,28587,47797,10078,25177,17354,49135,29691,43238,7280,52120,64621,42500,35920,35907,23823};

    int i,j,ans;
    for(i=0;i<5;++i){
        n=init[i].size()-1;
        for(j=1;j<=n;++j) 
	  	  a[j]=init[i][j];
        nw=BM::sol();
        for(j=0;j<=sz[i];++j)
        	poly[i][j]=nw[j];
	}
	scanf("%lld%lld",&K,&n);D=sz[--K];
	for(i=0;i<=D;++i) a[i]=poly[K][i];
	h[0]=1;f[1]=1;
	for(n--;n;n>>=1,mul(f,f))
		if(n&1) mul(h,f);
	for(ans=0,i=0;i<D;++i) 
	  ans=ad(ans,(ll)h[i]*init[K][i+1]%mod);
	printf("%d",ans);
    return 0;
}

你可能感兴趣的:(---线性代数---)