标签:
const int mod=2015; int N,M; struct Matrix{ int M[55][55]; Matrix(){memset(M,0,sizeof(M));} }U,P; Matrix Add(const Matrix &a,const Matrix &b){ Matrix ret; for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) ret.M[i][j]=(a.M[i][j]+b.M[i][j])%mod; return ret; } Matrix Multi(const Matrix &a,const Matrix &b){ Matrix ret; for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ for(int k=1;k<=N;k++) ret.M[i][j]+=a.M[i][k]*b.M[k][j]; ret.M[i][j]%=mod; } } return ret; } Matrix Pow(Matrix f,int n){ Matrix ret=U; while(n){ if(n&1) ret=Multi(ret,f); n>>=1; f=Multi(f,f); } return ret; } Matrix solve(int n){ if(n==1) return P; Matrix temp=solve(n>>1); if(n&1){ Matrix t=Pow(P,n/2+1); return Add(Add(Multi(temp,t),temp),t); } else return Add(temp,Multi(temp,Pow(P,n>>1))); }
标签:
原文地址:http://blog.csdn.net/qq_27599517/article/details/51891049