1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| typedef vector<vi> mat;
mat mul(mat a, mat b) {
int n = a.size(), m = b[0].size(), p = a[0].size();
mat c(n, vi(m));
for (int k = 0; k < p; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % MOD;
return c;
}
mat qpow(mat a, ll m) {
int n = a.size();
mat b(n, vi(n));
for (int i = 0; i < n; i++) b[i][i] = 1;
while (m) {
if (m & 1) b = mul(b, a);
a = mul(a, a);
m >>= 1;
}
return b;
}
|