题意
给出一个矩阵 A A A,求出 A k A^k Ak。
思路
因为矩阵乘法满足结合律,所以结合快速幂的方法可以快速求出答案。
代码
#include<cstdio>
#include<cstring>
const int mod = 1e9 + 7;
int N;
long long K;
struct matrix{
int a[101][101];
}result, a;
matrix operator *(matrix &a, matrix &b){
matrix c;
memset(c.a, 0, sizeof(c.a));
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
for (int k = 1; k <= N; k++)
c.a[i][j] = (c.a[i][j] + (long long)a.a[i][k] * b.a[k][j]) % mod;
return c;
}
int main() {
scanf("%d %lld", &N, &K);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
scanf("%d", &a.a[i][j]);
result = a;
for (K--; K; K >>= 1) {
if (K & 1) result = result * a;
a = a * a;
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++)
printf("%d ", result.a[i][j]);
printf("\n");
}
}