就是求一个矩阵的幂
快考试了时间太紧了没时间仔细搞
代码来自Qiu.YF
代码如下
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<string>
using namespace std;
#define in = read()
typedef long long ll;
typedef unsigned int ui;
const ll size = 100 +10;
ll n , k;
int mod = 1000000007;
inline ll read(){
ll num = 0 , f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num*10 + ch - '0';
ch = getchar();
}
return num*f;
}
struct Matrix{
ll s[size][size];
Matrix(){ memset(s , 0 , sizeof(s));}
Matrix operator *(const Matrix a){
Matrix ans;
for(register int i=1;i<=n;i++)
for(register int j=1;j<=n;j++)
for(register int k=1;k<=n;k++)
ans.s[i][j] = (ans.s[i][j] + s[i][k]*a.s[k][j]%mod)%mod;
return ans;
}
};
Matrix pow(Matrix q , ll opt){
ll i = opt;
Matrix ans;
for(register int j=1;j<=n;j++) ans.s[j][j] = 1;
while(i){
if(i&1) ans = ans*q;
q = q*q;
i >>= 1;
}
return ans;
}
int main(){
n in; k in;
Matrix start;
for(register int i=1;i<=n;i++)
for(register int j=1;j<=n;j++)
start.s[i][j] in;
Matrix ans = pow(start , k);
for(register int i=1;i<=n;i++,printf("\n"))
for(register int j=1;j<=n;j++)
printf("%lld " , ans.s[i][j]);
return 0;
}
//COYG