10求调
查看原帖
10求调
1340011
yufh楼主2025/8/4 19:24
#include <bits/stdc++.h>
using namespace std;
/*====================*/
typedef long long ll;
/*====================*/
#define endl '\n'
/*====================*/
const int N=1e2+10;
/*====================*/
const ll MOD=1e9+7;
/*====================*/
struct Matrix
{
	ll n,m;
	ll arr[N][N]; 
	Matrix(void) 
	{
		n=m=0;
		memset(arr,0,sizeof(arr));
	}
};
/*====================*/
Matrix operator* (const Matrix& x,const Matrix& y)
{
	Matrix ans;
	ans.n=x.n;
	ans.m=x.m;
	for(int i=1;i<=ans.n;i++)
	{
		for(int j=1;j<=ans.m;j++)
		{
			for(int k=1;k<=x.m;k++)
			{
				ans.arr[i][j]+=1LL*x.arr[i][k]*y.arr[k][j]%MOD;
			}
		}
	}
	return ans;
} 
/*====================*/
Matrix FastPow(Matrix a,ll b)
{
	Matrix ans;
	ans.n=a.n;
	ans.m=a.m;
	for (int i=1;i<=ans.n;i++) 
	{
        ans.arr[i][i]=1;
    }
	while(b)
	{
		if(b&1)
		{
			ans=ans*a;
		}
		a=a*a;
		b>>=1;
	}
	return ans;
}
/*====================*/
void Solve(void)
{
	ll n,k;cin>>n>>k;
	Matrix mat;
	mat.n=n,mat.m=n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>mat.arr[i][j];
		}
	}
	if(k==0)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(i==j) cout<<1<<" ";
				else cout<<0<<" ";
			}
			cout<<endl;
		}
		return;
	}
	mat=FastPow(mat,k);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cout<<mat.arr[i][j]%MOD<<" ";
		}
		cout<<endl;
	}
}
/*====================*/
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	int T=1;//cin>>T;
	while(T--) Solve();
	return 0;
}
2025/8/4 19:24
加载中...