#include <cstdio>
#include <cstring>
void mul(int a[][15], int b[][15], int ans[][15], int n) {
int tmp[15][15];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
tmp[i][j] = 0;
for (int k = 0; k < n; k++)
tmp[i][j] += a[i][k] * b[k][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
ans[i][j] = tmp[i][j];
}
void pow(int a[][15], int ans[][15], int n, int k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
ans[i][j] = 0;
else
ans[i][j] = 1;
while (k--)
mul(ans, a, ans, n);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, k;
int a[15][15], ans[15][15];
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);
pow(a, ans, n, k);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%d%c", ans[i][j], j==n-1?'\n':' ');
}
}
return 0;
}
九度OJ 1474 矩阵幂
最新推荐文章于 2021-05-23 06:26:07 发布