标签:
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Shanto is learning how to power up numbers and he found an efficient way to find kth power of a matrix. He was quite happy with his discovery. Suddenly his sister Natasha came to him and asked him to find the summation of the powers. To be specific his sister gave the following problem.
Let A be an n x n matrix. We define Ak = A * A * ... * A (k times). Here, * denotes the usual matrix multiplication. You are to write a program that computes the matrix A + A2 + A3 + ... + Ak.
Shanto smiled and thought that it would be an easy one. But after a while he found that it‘s tough for him. Can you help him?
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with two integers n (1 ≤ n ≤ 30) and k (1 ≤ k ≤ 109). Each of the next n lines will contain n non-negative integers (not greater than 10).
For each case, print the case number and the result matrix. For each cell, just print the last digit. See the samples for more details.
Sample Input |
Output for Sample Input |
2 3 2 1 4 6 6 5 2 1 2 3 3 10 1 4 6 6 5 2 1 2 3 |
Case 1: 208 484 722 Case 2: 868 620 546 |
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<math.h> 7 #include<queue> 8 using namespace std; 9 typedef long long LL; 10 typedef struct node 11 { 12 int m[70][70]; 13 node() 14 { 15 memset(m,0,sizeof(m)); 16 } 17 } maxtr; 18 int ans[70][70]; 19 void E(node *nn,int n); 20 maxtr ju(int n); 21 maxtr quick(node ju,int n,int m); 22 int main(void) 23 { 24 int i,j,k; 25 int n,m; 26 int s; 27 cin>>k; 28 for(s=1; s<=k; s++) 29 { 30 scanf("%d %d",&n,&m); 31 for(i=0; i<n; i++) 32 { 33 for(j=0; j<n; j++) 34 { 35 scanf("%d",&ans[i][j]); 36 ans[i][j]%=10; 37 } 38 }node aa;aa=ju(n); 39 aa=quick(aa,n,m);printf("Case %d:\n",s); 40 for(i=0;i<n;i++) 41 { 42 for(j=n;j<2*n;j++) 43 { 44 printf("%d",aa.m[i][j]); 45 }printf("\n"); 46 } 47 }return 0; 48 } 49 void E(node *nn,int n) 50 { 51 int i,j,k; 52 for(i=0; i<n; i++) 53 { 54 for(j=0; j<n; j++) 55 { 56 if(i==j) 57 nn->m[i][j]=1; 58 else nn->m[i][j]=0; 59 } 60 } 61 } 62 maxtr ju(int n) 63 { 64 int i,j,k; 65 maxtr nn; 66 for(i=0; i<n; i++) 67 { 68 for(j=0; j<n; j++) 69 { 70 nn.m[i][j]=ans[i][j]; 71 } 72 } 73 for(i=0; i<n; i++) 74 { 75 for(j=n; j<2*n; j++) 76 { 77 nn.m[i][j]=ans[i][j-n]; 78 } 79 } 80 node cc; 81 E(&cc,n); 82 for(i=n; i<2*n; i++) 83 { 84 for(j=n; j<2*n; j++) 85 { 86 nn.m[i][j]=cc.m[i-n][j-n]; 87 } 88 }return nn; 89 } 90 maxtr quick(node ju,int n,int m) 91 { node ee; 92 93 E(&ee,2*n); 94 int i,j,k; 95 int s; 96 while(m) 97 { 98 if(m&1) 99 { 100 node cc; 101 for(i=0; i<2*n; i++) 102 { 103 for(j=0; j<2*n; j++) 104 { 105 for(s=0; s<2*n; s++) 106 { 107 cc.m[i][j]=(ju.m[i][s]*ee.m[s][j]+cc.m[i][j])%10; 108 } 109 } 110 } 111 ee=cc; 112 } 113 node cc; 114 for(i=0; i<2*n; i++) 115 { 116 for(j=0; j<2*n; j++) 117 { 118 for(s=0; s<2*n; s++) 119 { 120 cc.m[i][j]=(ju.m[i][s]*ju.m[s][j]+cc.m[i][j])%10; 121 } 122 } 123 } 124 ju=cc; 125 m/=2; 126 } 127 return ee; 128 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5494718.html