https://www.luogu.org/problemnew/show/P3390
快速幂 + 矩阵
#include <bits/stdc++.h> using namespace std; #define LL long long const LL mod = 1e9 + 7; const int N = 110; LL n, k; struct JZ{ LL m[N][N]; JZ() {memset(m,0,sizeof m);} void clear() {for(int i = 1; i <= 100; i ++) m[i][i] = 1;} }; JZ operator *(JZ a, JZ b) { JZ ret; for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) for(int k = 1; k <= n; k ++) ret.m[i][j] = (ret.m[i][j] + a.m[i][k] * b.m[k][j]) % mod; return ret; } JZ ksm(JZ a, LL p){ JZ ret; ret.clear(); while(p){ if(p & 1) ret = ret * a; a = a * a; p >>= 1; } return ret; } int main() { cin >> n >> k; JZ a; for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) cin >> a.m[i][j]; JZ Ans = ksm(a, k); for (int i=1; i<=n; i++,puts("")) for (int j=1; j<=n; j++) printf("%lld ",Ans.m[i][j]); return 0; }