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
1#include <iostream>2#include <string.h>3 using namespacestd;4 intMain () {5 inta[ *][ *],n,m,b[ *][ *],c[ *][ *];6Cin>>N;7Cin>>m;8 for(inti =0; I < n;i++){9 for(intj =0; J < n;j++)TenCin>>A[i][j]; One } A -memset (C,0,sizeof(C)); - if(m) { the for(inti =0; I < n;i++){ - for(intj =0; J < n;j++) -B[I][J] =A[i][j]; - } + for(inti =0; I < M-1; i++){ - for(intj =0; J < n;j++){ + for(intK =0; k < n;k++){ A for(intx =0; x < n;x++) atC[j][k] + = b[j][x] *A[x][k]; - } - } - for(intA =0; a < n;a++){ - for(intb =0; b < n;b++) -B[A][B] =C[a][b]; in } -memset (C,0,sizeof(C)); to } + - } the Else{ *memset (B,0,sizeof(B)); $ for(inti =0; I < n;i++){Panax NotoginsengB[i][i] =1; - } the } + for(inti =0; I < n;i++){ A for(intj =0; J < n;j++) thecout<<b[i][j]<<' '; +cout<<Endl; - } $ return 0; $}
View Code
Matrix 0 Power is the unit matrix to take into account
Basic exercise matrix multiplication time limit: 1.0s memory limit: 512.0MB