-
Title Description:
-
Given a n*n matrix, the K-Power of the matrix, i.e. P^k, is obtained.
-
Input:
-
The input contains multiple sets of test data.
The first behavior of the data is an integer T (0<t<=10), which represents the number of required matrices.
Next there is the T Group test data, each set of data formats as follows:
The first line: two integers n (2<=n<=10), K (1<=k<=5), two digits separated by a space, meaning as shown above.
Next there are n rows, n positive integers per line, where the J integer of line I is the matrix element Pij and (0<=pij<=10) of row J of column I in the Matrix. In addition, the data guarantees that the final result will not exceed 10^8.
-
Output:
-
For each set of test data, output its results. The format is:
n rows n columns of integers, separated by a space between each line, note that there should be no extra space after the last number of rows.
-
Sample input:
-
32 29 89 33 34 8 49 3 03 5 75 24 0 3 0 10 0 5 8 58 9 8 5 39 6 1 7 87 2 5 7 3
-
Sample output:
-
153 96108 811216 1248 7081089 927 5041161 1151 73947 29 41 22 16147 103 73 116 94162 108 153 168 126163 67 112 158 122152 93 93 111 97
1#include <iostream>2 using namespacestd;3 //For i=0 to N-14 //For j=0 to N-15 //For K6 //c[i][j]=sum (b[i][0 to N-1]*a[0 to N-1][j])7 structShenme8 {9 intm[Ten][Ten];Ten }; One structShenme arry[Ten]; A structShenme Multiply (structShenme A,structShenme B,intN) - { - structShenme C; the for(intI=0; i<n;i++) - { - for(intj=0; j<n;j++) - { +c.m[i][j]=0; - for(intk=0; k<n;k++) + { Ac.m[i][j]+=a.m[i][k]*B.m[k][j]; at - } - - } - - } in returnC; - to } + voidInite (intNintt) - { the for(intI=0; i<n;i++) * for(intj=0; j<n;j++) $ {Panax NotoginsengCin>>Arry[t].m[i][j]; - } the } + structShenme Jiecheng (intKintTintN) A { the structShenme temp; + for(intI=0; i<n;i++) - for(intj=0; j<n;j++) $temp.m[i][j]=Arry[t].m[i][j]; $ for(intI=2; i<=k;i++) - { -Temp=multiply (Arry[t],temp,n);//two structures passed in the } - returntemp;Wuyi } the void out(structShenme result,intN) - { Wu for(intI=0; i<n;i++) - { About for(intj=0; j<n;j++) $ { - if(j!=n-1) -cout<<result.m[i][j]<<" "; - Else Acout<<Result.m[i][j]; + } thecout<<Endl; - } $ } the intMain () the { the intT//Enter a few arrays? (Structural body) theCin>>T; - for(intt=0; t<t;t++)//The first few arrays of T in { the intn,k; theCin>>n>>K; About //Array Initialization the Inite (n,t); the structShenme result; theresult=Jiecheng (k,t,n); + out(result,n); - } the return 0;Bayi}
Topic 1474: Power of the Matrix