51Nod 1113 矩阵快速幂

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 typedef long long ll;
 8 int n, m;
 9 const int maxn = 105;
10 const int MOD = 1e9 + 7;
11 
12 struct node{
13     ll a[maxn][maxn];
14 };
15 
16 node A, ans;
17 
18 node multi(node x, node y){
19     node z;
20     for(int i = 1;i <= n;i++){
21         for(int j = 1;j <= n;j++){
22             z.a[i][j] = 0;
23             for(int k = 1;k <= n;k++){
24                 z.a[i][j] += x.a[i][k]*y.a[k][j]%MOD;
25                 z.a[i][j] %= MOD;
26             }
27         }
28     }
29     return z;
30 }
31 
32 void f(){
33     while(m > 0){
34         if(m%2){
35             //ans = ans*a%MOD;
36             ans = multi(ans, A);
37         }
38         //a = (a*a)%MOD;
39         A = multi(A,A);
40         m /= 2;
41     }
42 }
43 
44 int main(){
45     ios_base::sync_with_stdio(false);
46     cin.tie(0);
47     cin >> n >> m;
48     for(int i = 1;i <= n;i++){
49         for(int j = 1;j <= n;j++){
50             cin >> A.a[i][j];
51             ans.a[i][j] = A.a[i][j];
52         }
53     }
54     m--;
55     f();
56     for(int  i = 1;i <= n;i++){
57         for(int j = 1;j<=n;j++)
58         {
59             if(j == 1){
60                 cout << ans.a[i][j];
61             }
62             else{
63                 cout << " " << ans.a[i][j];
64             }
65         }
66         cout << endl;
67     }
68     return 0;
69 }

 

posted @ 2018-05-14 19:08  ouyang_wsgwz  阅读(130)  评论(0编辑  收藏  举报