应该还蛮好理解的。。
1 #include <cstdio> 2 using namespace std; 3 typedef long long LL; 4 5 const LL maxn=105, prime=1e9+7; 6 LL n, k, mat[maxn][maxn]; 7 LL ans[maxn][maxn], tmp[maxn][maxn], tmp2[maxn][maxn]; 8 9 void plus(LL a[maxn][maxn], LL b[maxn][maxn]){ 10 for (LL i=0; i<n; ++i) for (LL j=0; j<n; ++j){ 11 tmp2[i][j]=0; 12 for (LL k=0; k<n; ++k) 13 tmp2[i][j]=(tmp2[i][j]+(a[i][k]*b[k][j])%prime)%prime; 14 } 15 for (LL i=0; i<n; ++i) for (LL j=0; j<n; ++j) 16 a[i][j]=tmp2[i][j]; 17 } 18 19 int main(){ 20 scanf("%lld%lld", &n, &k); 21 for (LL i=0; i<n; ++i) for (LL j=0; j<n; ++j) 22 scanf("%lld", &mat[i][j]), tmp[i][j]=mat[i][j]; 23 for (LL i=0; i<n; ++i) for (LL j=0; j<n; ++j) 24 if (i==j) ans[i][j]=1; 25 while (k){ 26 if (k&1) plus(ans, tmp); 27 plus(tmp, tmp); 28 k>>=1; 29 } 30 for (LL i=0; i<n; ++i){ 31 for (LL j=0; j<n; ++j) 32 printf("%lld ", ans[i][j]); 33 printf(" "); 34 } 35 return 0; 36 }