Power to the matrix (commissioning cost a great effort)

Source: Internet
Author: User

Make a little progress every day ...

The title describes a n*n matrix, which is called the K-power of the Matrix, that is, p^k.
Input Description:
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.

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 Description:
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.

Input Example:
3
2 2
9 8
9 3
3 3
4 8 4
9 3 0
3 5 7
5 2
4 0 3) 0 1
0 0 5) 8 5
8 9 8) 5 3
9 6 1) 7 8
7 2 5) 7 3

Output Example:
153 96
108 81
1216 1248 708
1089 927 504
1161 1151 739
47 29 41) 22 16
147 103 73) 116 94
162 108 153) 168 126
163 67 112) 158 122
152 93 93) 111 97


Code:
ImportJava.util.Scanner; Public classpower of the matrix { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in);  while(In.hasnext ()) {intCount =In.nextint ();  for(inti = 0; I < count; i++) {                intn = in.nextint ();//n*n Matrix                intK = In.nextint ();//Power                if(n > 0 && n <= 10) {                    intNum[][] =New int[N][n];  for(intA = 0; a < n; a++) {                         for(intb = 0; b < n; b++) {Num[a][b]=In.nextint (); }                    }                    intNum1[][] =num;  for(intj = 0; J < K-1; J + +) {NUM1=juzhen (n, num, NUM1); }                     for(intA = 0; a < n; a++) {                         for(intb = 0; b < n; b++) {System.out.print (Num1[a][b]+ " "); }                        if(A < N-1) System.out.println ();    }}}} in.close (); }     Public Static int[] Juzhen (intNintNum[][],intnum1[][]) {        intNum2[][] =New int[N][n]; intsum = 0;  for(inti = 0; I < n; i++) {             for(intj = 0; J < N; J + +) {                 for(intA = 0; a < n; a++) {                    //System.out.println (Num1[i][a] + "multiply" + num[a][j]);Sum + = num1[i][a] *Num[a][j]; //System.out.println (sum);} Num2[i][j]=sum; Sum= 0; }        }        returnnum2; }}

Power to the matrix (commissioning cost a great effort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.