Basic exercise matrix multiplication time limit: 1.0s memory Limit: 512.0MB problem description given an n-order matrix A, the M power of output A (m non-negative integer)
For example:
A =
1 2
3 4
A power of 2 times
7 10
15 22 input Format the first line is a positive integer N, M (1<=n<=30, 0<=m<=5), representing the order of the matrix A and the required power number
Next n rows, each row n absolute value does not exceed 10 non-negative integer, the output format of the description matrix A outputs a total of n rows, n integers per row, representing the matrix corresponding to the M power of a. Separate sample input with a space between adjacent numbers 2 2
1 2
3 4 Sample Output 7 10
15 22
Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner Scanner = new Scanner (system.i n); while (Scanner.hasnext ()) {int n = scanner.nextint (); int m = Scanner.nextint (); int[][] init = new Int[n][n];for (int i = 0; I < n; i++) {for (int j = 0; J < N; j + +) {Init[i][j] = Scanner.nextint ();}} Int[][] result = new Int[n][n];for (int i = 0; i < n; i++) {result[i][i] = 1;} Poweroperation (result, init, m);p Rintmatrix (result);}} private static void Printmatrix (int[][] result) {int len = result.length;for (int i = 0; i < len; i++) {for (int j = 0; J < Len; J + +) {System.out.print (result[i][j]); System.out.print (j = = len-1?) "\ r \ n": "");}}} private static void Poweroperation (int[][] result, int[][] init, int m) {int len = init.length;for (int i = 0; i < m; I + +) {int[][] temp = new Int[len][len];for (int j = 0; J < Len; J + +) {for (int j2 = 0, J2 < len; j2++) {for (int k = 0; K < Len; k++) {Temp[j][j2] + = result[j][k] * init[k][j2];}}} For(int j = 0; J < Len; j + +) {for (int j2 = 0; J2 < len; j2++) {result[j][j2] = temp[j][j2];}}}}