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 The problem is very simple, and the amount of data is very small, direct violence calculation, it should be possible, but I still intend to use its standard solution, matrix fast to optimize its time complexity
#include <iostream>#include<string.h>using namespacestd;structm{intnum[ +][ +]; M () {memset (num,0,sizeof(num)); }}; M a,e;intm; M Mul (M a,m b) {//compute matrix multiplication M C; for(intI=0; i<m;i++){ for(intj=0; j<m;j++){ for(intk=0; k<m;k++) {C.num[i][j]+ = (a.num[i][k]*B.num[k][j]); } } } returnC;} M multi (M C,intN) {//Matrix Fast Ghost Core code M b=c,r=e; while(n) {if(n&1) {R=Mul (R,B); } b=Mul (B,B); N>>=1; } returnR;}intMain () {intN; CIN>>m>>N; for(intI=0; i<m;i++) {E.num[i][i]=1; for(intj=0; j<m;j++) {cin>>A.num[i][j]; }} M x=multi (a,n); for(intI=0; i<m;i++){ for(intj=0; j<m;j++) {cout<<x.num[i][j]<<" "; } cout<<Endl; } return 0;}
Blue Bridge cup basic_17 matrix multiplication (Matrix fast power)