HDU 1575 Tr A
题意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
思路:基础矩阵题。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <string> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8 const int N=11; 9 10 typedef struct Node{ 11 int m[N][N]; 12 }Matrix; 13 Matrix init,unit; 14 int n,k; 15 16 void Init(){ 17 scanf("%d%d",&n,&k); 18 for(int i=1;i<=n;i++){ 19 for(int j=1;j<=n;j++){ 20 scanf("%d",&init.m[i][j]); 21 unit.m[i][j]=(i==j); 22 } 23 } 24 } 25 26 Matrix Mul(Matrix a,Matrix b){ 27 int i,j; 28 Matrix c; 29 for(i=1;i<=n;i++){ 30 for(j=1;j<=n;j++){ 31 c.m[i][j]=0; 32 for(int k=1;k<=n;k++) 33 c.m[i][j]+=a.m[i][k]*b.m[k][j]; 34 c.m[i][j]%=9973; 35 } 36 } 37 return c; 38 } 39 40 Matrix pow(Matrix a,Matrix b){ 41 while(k>1){ 42 if(k&1){ 43 b=Mul(a,b); 44 k--; 45 } 46 else{ 47 a=Mul(a,a); 48 k>>=1; 49 } 50 } 51 a=Mul(a,b); 52 return a; 53 } 54 55 void Debug(Matrix x){ 56 for(int i=1;i<=n;i++){ 57 for(int j=1;j<=n;j++) 58 printf("%d ",x.m[i][j]); 59 puts(""); 60 } 61 } 62 63 int main(){ 64 65 // freopen("data.in","r",stdin); 66 // freopen("data.out","w",stdout); 67 68 int t; 69 scanf("%d",&t); 70 while(t--){ 71 Init(); 72 Matrix x; 73 x=pow(init,unit); 74 int sum=0; 75 // Debug(x); 76 while(n){ 77 sum+=x.m[n][n]; 78 sum%=9973; 79 n--; 80 } 81 printf("%d\n",sum); 82 } 83 return 0; 84 }