矩阵快速幂
emm
没错这就是矩阵快速幂的裸体板子题
所以
没什么可说的哇qwq
矩阵乘 + 快速幂 (+ 预处理)
1 #include<cstdio>
2 #include<cstring>
3 #define ll long long
4 using namespace std;
5 const ll mod = 1e9+7;
6 ll n,k;
7 struct mat
8 {
9 ll m[105][105];
10 }l,r;
11 mat mul(mat x,mat y)
12 {
13 mat sum;
14 for(int i = 1;i <= n;i++)
15 for(int j = 1;j <= n;j++)
16 sum.m[i][j] = 0;
17 for(int i = 1;i <= n;i++)
18 for(int j = 1;j <= n;j++)
19 for(int k = 1;k <= n;k++)
20 sum.m[i][j] = (sum.m[i][j]%mod +(x.m[i][k] * y.m[k][j])% mod)% mod;
21 return sum;
22 }
23 mat qpow(mat x,ll y)
24 {
25 mat ans = r;
26 while(y)
27 {
28 if(y & 1)
29 ans = mul(ans,x);
30 x = mul(x,x);
31 y >>= 1;
32 }
33 return ans;
34 }
35 int main()
36 {
37 scanf("%lld%lld",&n,&k);
38 for(int i = 1;i <= n;i++)
39 for(int j = 1;j <= n;j++)
40 scanf("%lld",&l.m[i][j]);
41 for(int i = 1;i <= n;i++)
42 r.m[i][i] = 1;
43 mat ans = qpow(l,k);
44 for(int i = 1;i <= n;i++)
45 {
46 for(int j = 1;j <= n;j++)
47 printf("%lld ",ans.m[i][j]);
48 printf("\n");
49 }
50 return 0;
51 }