#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int a[100][100] = {0}, b[100][100] = {0}, c[100][100] = {0}, d[100][100], temp[100][100];
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
if (k == 0)//0次幂直接输出
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
cout << 1 << " ";
else
{
cout << 0 << " ";
}
}
cout << endl;
}
}
if (k == 1)//1次幂输出a矩阵
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout<<a[i][j]<<" ";
}
cout << endl;
}
}
int flag=0;
while (k)
{
if((k&1)&&(!flag)){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
c[i][j]=b[i][j];
}
}
flag=1;
}
else if ((k & 1)&&(flag))
{
for (int i = 0; i < n; i++)
{ // 暂存c的矩阵
for (int j = 0; j < n; j++)
{
temp[i][j] = c[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
c[i][j] = 0;
for (int k = 0; k < n; k++)
{
c[i][j] += ((b[i][k] * temp[k][j])% (int(1e9)+7));
c[i][j]%=(1000000000+7);
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
temp[i][j] = b[i][j];
}
}
for (int i = 0; i < n; i++)//b矩阵是次方矩阵,即124816
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
b[i][j] += ((temp[i][k] * temp[k][j])% (1000000000+7));
}
}
}
}
for (int i = 0; i < n; i++)//输出所需的c矩阵
{
for (int j = 0; j < n; j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
这题没有写出来,对我来说难度一点大,过段时间再回头来看这道题