题意简述
给定 ( n*n )的矩阵A,求 ( A^k )
代码
#include <cstdio>
typedef long long ll;
const int mod = 1000000007;
int n;
ll k;
struct Matrix
{
int a[200][200];
Matrix& operator =(const Matrix& x)
{
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
a[i][j] = x.a[i][j];
return *this;
}
};
Matrix a, b;
Matrix Mul(const Matrix& x, const Matrix& y)
{
Matrix s;
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
s.a[i][j] = 0;
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
for (register int k = 1; k <= n; ++k)
s.a[i][j] = (s.a[i][j] + (ll)x.a[i][k] * y.a[k][j] % mod) % mod;
return s;
}
Matrix _pow(Matrix x, ll y)
{
Matrix s;
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
s.a[i][j] = (i == j);
for (; y; y >>= 1, x = Mul(x, x)) if (y & 1) s = Mul(s, x);
return s;
}
int main()
{
scanf("%d%lld", &n, &k);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
scanf("%d", &a.a[i][j]);
b = _pow(a, k);
for (register int i = 1; i <= n; ++i)
{
for (register int j = 1; j < n; ++j)
printf("%d ", b.a[i][j]);
printf("%d
", b.a[i][n]);
}
}