[luoguP3390]【模板】矩阵快速幂

传送门

模板不解释。

——代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #define LL long long
 4 
 5 int n;
 6 LL k;
 7 const int p = 1e9 + 7;
 8 
 9 struct Matrix
10 {
11     LL a[100][100];
12     Matrix()
13     {
14         memset(a, 0, sizeof(a));
15     }
16 };
17 
18 inline Matrix operator * (const Matrix x, const Matrix y)
19 {
20     Matrix ans;
21     int i, j, k;
22     for(i = 0; i < n; i++)
23         for(j = 0; j < n; j++)
24             for(k = 0; k < n; k++)
25                 ans.a[i][j] = (ans.a[i][j] + x.a[i][k] * y.a[k][j]) % p;
26     return ans;
27 }
28 
29 int main()
30 {
31     int i, j;
32     Matrix ans, trs;
33     scanf("%d %lld", &n, &k);
34     for(i = 0; i < n; i++) ans.a[i][i] = 1;
35     for(i = 0; i < n; i++)
36         for(j = 0; j < n; j++)
37             scanf("%lld", &trs.a[i][j]);
38     while(k)
39     {
40         if(k & 1) ans = ans * trs;
41         trs = trs * trs;
42         k >>= 1;
43     }
44     for(i = 0; i < n; i++)
45     {
46         for(j = 0; j < n; j++) printf("%d ", ans.a[i][j]);
47         puts("");
48     }
49     return 0;
50 }
View Code
原文地址:https://www.cnblogs.com/zhenghaotian/p/6844170.html