//
// Created by Sieak Softly on 2019-12-16.
//
#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "sstream"
#include "cstring"
#include "cmath"
#include "algorithm"
#include "queue"
#include "map"
#include "vector"
#include "stack"
#include "set"
#include "climits"
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
const int INF = INT_MAX;
const int mod = 1e9 + 7;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n;
struct mat {
ll m[maxn][maxn];
} unit;
mat operator * (mat a, mat b) {
mat res;
ll x;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
x = 0;
for (int k = 0; k < n; ++k) x = (x + a.m[i][k] * b.m[k][j]) % mod;
res.m[i][j] = x;
}
}
return res;
}
ostream & operator << (ostream & os, const mat a) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) os << a.m[i][j] << (j < n - 1 ? " " : "\n");
return os;
}
istream & operator >> (istream & is, mat & a) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) is >> a.m[i][j];
return is;
}
void init_unit() {
for (int i = 0; i < n; ++i) unit.m[i][i] = 1;
}
mat mat_quick_pow(mat a, ll k) {
mat res = unit;
while (k) {
if (k & 1) res = res * a;
a = a * a;
k >>= 1;
}
return res;
}
int main() {
ll k;
scanf("%d%lld", &n, &k);
init_unit();
mat a;
cin >> a;
cout << mat_quick_pow(a, k);
return 0;
}
矩阵快速幂
最新推荐文章于 2024-11-16 21:36:47 发布