Matrix Multiplication
Problem Description
Given ⼀ N- order matrix A , the output A of the M -th power ( M is an integer comes in handy negative)
E.g:
A = 1 2
3 4
A is a power of 2 710
15 22
START input format
⾏ ⼀ first frame is positive integer N , M ( . 1 <= N <= 30, 0 <= M <=. 5), the matrix represents A a power of order and claimed
Next N ⾏, each ⾏ N absolute values does not exceed 10 to comes in handy negative integer, described matrix A value
Output Format
The output common N ⾏, each ⾏ N integers, indicates A of M -th power corresponding to the matrix. Using several adjacent spaces separated ⼀
Sample lose START
2 2
1 2
3 4
Sample Output
7 10
15 22
Analysis: There are actually a ⼀! 0 power! Ming matrix to zero output matrix!
#include <iostream>
using namespace std;
long long int b[40][40];
int main() {
int n, m;
cin >> n >> m;
long long int a[40][40];
long long int t[40][40];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> a[i][j];
t[i][j] = a[i][j];
}
}
if( m == 0) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i != j) {
cout << 0 << " ";
} else {
cout << 1 << " ";
}
}
cout << endl;
}
return 0;
}
while(--m) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int k = n;
while(k) {
b[i][j] += t[i][k-1] * a[k-1][j];
k--;
}
}
}
for( int i = 0; i < n; i++) {
for( int j = 0; j < n; j++) {
t[i][j] = b[i][j];
b[i][j] = 0;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << t[i][j] << " ";
}
cout << endl;
}
return 0;
}