Untitled

mail@pastecode.io avatar
unknown
plain_text
10 months ago
1.1 kB
1
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

#define MAX_N 100
#define MOD 100000007

struct Matrix {
	long long mat[MAX_N][MAX_N];
};

int T, tc;
int N, M;
Matrix m;
Matrix res;

Matrix mat_mul(Matrix A, Matrix B)
{
	Matrix res;
	for(int i=0; i<N; i++){
		for(int j=0; j<N; j++){
			res.mat[i][j]=0;
			for(int k=0; k<N; k++){
				res.mat[i][j] = (res.mat[i][j] + A.mat[i][k] * B.mat[k][j]) % MOD;
			}
		}
	}
	return res;
}

void mat_pow(int exp)
{
	if(exp==1)
		return;

	mat_pow(exp/2);
	res = mat_mul(res, res);

	if(exp%2!=0)
		res = mat_mul(res, m);
}

int main()
{
	freopen("input.txt", "r", stdin);
	cin >> T;
	for(tc=1; tc<=T; tc++)
	{
		cin >> N >> M;
		for(int i=0; i<N; i++){
			for(int j=0; j<N; j++){
				cin >> m.mat[i][j];
				res.mat[i][j] = m.mat[i][j];
			}
		}
		mat_pow(M);
		cout << "Case #" << tc << endl;
		for(int i=0; i<N; i++){
			for(int j=0; j<N; j++){
				cout << res.mat[i][j] << " ";
			}
			cout << endl;
		}
	}
	return 0;
}
Leave a Comment