1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| // Problem: P3390 【模板】矩阵快速幂 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P3390 // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h> using namespace std; #define ll long long # define int long long #define ull unsigned long long #define pii pair<int,int>
#define baoliu(x, y) cout << fixed << setprecision(y) << x #define endl "\n" #define debug1(x) cerr<<x<<" " #define debug2(x) cerr<<x<<endl const int N = 2e5 + 10; const int M = 1e6 + 10; const int inf = 0x3f3f3f3f; const int mod =1e9+7; const double eps = 1e-8; int n, m; int k; struct matrix{ int c[101][101]; matrix(){memset(c,0,sizeof c);} }; matrix operator*(matrix &a,matrix &b){ matrix res; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ res.c[i][j]+=(a.c[i][k]*b.c[k][j])%mod; res.c[i][j]%=mod; } } } return res; } matrix pow(matrix &a,int k){ matrix res; for(int i=1;i<=n;i++)res.c[i][i]=1; while(k){ if(k&1)res=(res*a); a=a*a; k>>=1; } return res; } void solve(){ cin>>n>>k; matrix a; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a.c[i][j]; //cerr<<a.c[i][j]<<" "; } //cerr<<endl; } matrix ans=pow(a,k); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<ans.c[i][j]<<" "; } cout<<endl; } } signed main() { cin.tie(0); ios::sync_with_stdio(false);
int t; //cin>>t; t=1; while (t--) { solve(); } return 0; }
|