(Hdu step 8.3.1) Tr A (matrix Rapid power -- result of finding the n-power trace % k of matrix m)
Question:
Tr |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission (s): 66 Accepted Submission (s): 57 |
|
If Problem DescriptionA is A square matrix, Tr A indicates the trace of A (the sum of all items on the main diagonal). Tr (A ^ k) % 9973 is required. |
The first line of Input data is a T, indicating that T groups of data exist. The first row of each data group contains n (2 <= n <= 10) and k (2 <= k <10 ^ 9) data. Next there are n rows, each row has n data, and the range of each data is [], which indicates the content of square matrix. |
Output Output Tr (A ^ k) % 9973 for each group of data. |
Sample Input22 21 00 13 999999991 2 34 5 67 8 9 |
Sample Output22686 |
Authorxhd |
SourceHDU 2007-1 Programming Contest |
Recommendlinle |
Question Analysis:
The rapid power of the matrix.
The following explains why there is a quick power method (which is purely personal and may not be accurate ).
We often encounter the following requirement: "evaluate the B-power modulo k of ". When both a and B are large, the result of a common method may be beyond the range expressed by an integer in C/C ++. At this time, we will use the matrix's quick power.
The quick power template for numbers is as follows:
// m^n % kint quickpow(int m,int n,int k){ int b = 1; while (n > 0) { if (n & 1) b = (b*m)%k; n = n >> 1 ; m = (m*m)%k; } return b;}
The quick power template for the matrix is as follows:
Struct Mat {int mat [N] [N] ;};/*** multiply the matrix. * returns the result of matrix a * matrix B */Mat operator * (Mat a, Mat B) {Mat c; memset (c. mat, 0, sizeof (c. mat); int I, j, k; for (I = 0; I <n; ++ I) {for (j = 0; j <n; ++ j) {for (k = 0; k <n; ++ k) {c. mat [I] [j] + =. mat [I] [k] * B. mat [k] [j];} c. mat [I] [j] % = 9973; // This is based on the question, and each of the result matrices should be % 9973, otherwise it may overflow} return c ;} /*** calculate the power of the matrix * returns a ^ k POWER */Mat operator ^ (Mat a, int k) {Mat c; int I, j; for (I = 0; I <n; ++ I) {for (j = 0; j <n; ++ j) {c. mat [I] [j] = (I = j); // The unit matrix is initialized} // fast power algorithm for (; k> = 1) {if (k & 1) {c = c * a;} a = a * a;} return c;}/*** calculate the trace of the matrix. ** add the number on the diagonal line of the matrix. **/int getTr (Mat a, int n) {int I; int sum = 0; for (I = 0; I <n; ++ I) {sum + =. mat [I] [I]; // Add the following sum % = 9973 to the number on the diagonal line of the matrix; // prevent number overflow. Every number has a modulo} return sum ;}
The Code is as follows:
/** A. cpp ** Created on: March 25, 2015 * Author: Administrator */# include
# Include
# Include
Using namespace std; const int N = 11; int n; struct Mat {int mat [N] [N] ;};/*** multiply the matrix. * returns the result of matrix a * matrix B */Mat operator * (Mat a, Mat B) {Mat c; memset (c. mat, 0, sizeof (c. mat); int I, j, k; for (I = 0; I <n; ++ I) {for (j = 0; j <n; ++ j) {for (k = 0; k <n; ++ k) {c. mat [I] [j] + =. mat [I] [k] * B. mat [k] [j];} c. mat [I] [j] % = 9973; // This is based on the question, and each of the result matrices should be % 9973, otherwise it may overflow} return c ;} /*** calculate the power of the matrix * returns a ^ k POWER */Mat operator ^ (Mat a, int k) {Mat c; int I, j; for (I = 0; I <n; ++ I) {for (j = 0; j <n; ++ j) {c. mat [I] [j] = (I = j); // The unit matrix is initialized} // fast power algorithm for (; k> = 1) {if (k & 1) {c = c * a;} a = a * a;} return c;}/*** calculate the trace of the matrix. ** add the number on the diagonal line of the matrix. **/int getTr (Mat a, int n) {int I; int sum = 0; for (I = 0; I <n; ++ I) {sum + =. mat [I] [I]; // Add the following sum % = 9973 to the number on the diagonal line of the matrix; // prevent number overflow. Every number has a modulo} return sum ;} int main () {int t; scanf ("% d", & t); while (t --) {int k; scanf ("% d", & n, & k); Mat m; int I; int j; for (I = 0; I <n; ++ I) {for (j = 0; j <n; ++ j) {scanf ("% d", & m. mat [I] [j]) ;}} m = m ^ k; // This is the usage of the matrix k power printf ("% d \ n ", getTr (m, n);} return 0 ;}