ma tran

 avatar
unknown
plain_text
2 years ago
1.1 kB
3
Indexable
#include <iostream>
using namespace std;
long long A[101][101];
long long res[101][101];
void nhan(long long a[101][101],long long b[101][101], int n){
	long long B[101][101];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			long long tong = 0;
			for(int k = 0; k < n; k++){
				tong = tong + a[i][k]*b[k][j];
			}
			B[i][j] = tong % 100000007;
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			res[i][j] = B[i][j];
		}
	}
}
void tinh(int m, int n){
	if(m == 1){
		return ;
	}
	else{
		tinh(m/2, n);
		nhan(res,res,n);
		if( m %2 == 1){
			nhan(res,A,n);
		}
	}
}
int main(){
	freopen("input.txt", "r", stdin);
	int T;
	cin >> T;
	for(int tc = 1; tc <= T; tc++){
		int n,m;
		cin >> n >> m;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cin >> A[i][j];
				res[i][j] = A[i][j];
			}
		}
		tinh(m,n);
		cout << "Case #" << tc << endl;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cout << res[i][j] << " ";
			}
			cout << endl;
		}

	}
	return 0;
}
Editor is loading...