You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
# define int long long
# define endl "\n"
const int MOD = 1e9 + 7 ;
const int N = 110 ;
int n , k ; // 本题数据范围很大, 用int直接wa哭了
int a [ N ] [ N ] , b [ N ] [ N ] ; // 原始矩阵,结果矩阵
// 矩阵乘法模板
void mul ( int a [ ] [ N ] , int b [ ] [ N ] , int c [ ] [ N ] ) {
int t [ N ] [ N ] = { 0 } ;
for ( int i = 0 ; i < N ; i + + )
for ( int j = 0 ; j < N ; j + + )
for ( int k = 0 ; k < N ; k + + )
t [ i ] [ j ] = ( t [ i ] [ j ] + a [ i ] [ k ] * b [ k ] [ j ] % MOD ) % MOD ; // 矩阵乘法
memcpy ( c , t , sizeof t ) ;
}
signed main ( ) {
cin > > n > > k ;
// 输入原始矩阵
for ( int i = 0 ; i < n ; i + + )
for ( int j = 0 ; j < n ; j + + )
cin > > a [ i ] [ j ] ;
// 1、单位矩阵
for ( int i = 0 ; i < n ; i + + ) b [ i ] [ i ] = 1 ;
// 2、矩阵快速幂
for ( int i = k ; i ; i > > = 1 ) {
if ( i & 1 ) mul ( a , b , b ) ;
mul ( a , a , a ) ;
}
// 3、输出
for ( int i = 0 ; i < n ; i + + ) {
for ( int j = 0 ; j < n ; j + + )
printf ( " %lld " , b [ i ] [ j ] ) ;
printf ( " \n " ) ;
}
}