HDU 1575 Tr A
矩阵快速幂
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=15; const int MOD=9973; int n,k; struct Matrix { int matrix[maxn][maxn]; Matrix operator*(Matrix b); }; Matrix Matrix::operator*(Matrix b) { Matrix g; int i,j,k; for(i=1; i<=n; i++) for(j=1; j<=n; j++) { int sum=0; for(k=1; k<=n; k++) sum=(sum+(matrix[i][k]*b.matrix[k][j])%MOD)%MOD; g.matrix[i][j]=sum; } return g; } int main() { int T; scanf("%d",&T); while(T--) { int i,j; scanf("%d%d",&n,&k); Matrix a; for(i=1; i<=n; i++) for(j=1; j<=n; j++) scanf("%d",&a.matrix[i][j]); Matrix c; for(i=1; i<=n; i++) { for(j=1; j<=n; j++) { if(i==j) c.matrix[i][j]=1; else c.matrix[i][j]=0; } } while(k!=0) { if(k%2==1) c=c*a,k--; else a=a*a,k=k/2; } int ans=0; for(i=1; i<=n; i++) ans=(ans+c.matrix[i][i])%MOD; printf("%d\n",ans); } return 0; }