站外题 90pts 求助
查看原帖
站外题 90pts 求助
1145420
longyitongxue楼主2025/7/31 15:15

#include<iostream>
#include<string.h>
#define int long long
using namespace std;
int n,mod;
int a[1005][1005],b[1005][1005],c[1005][1005];
void mul(int x[1005][1005],int y[1005][1005]){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			c[i][j]=0;
			for(int k=1;k<=n;k++){
				c[i][j]=(c[i][j]+x[i][k]*y[k][j])%mod;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			x[i][j]=c[i][j];
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	int m;
	cin>>n>>m>>mod;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
		a[i][i+n]=a[i+n][i+n]=b[i][i]=b[i+n][i+n]=1;
	}
	n*=2;
	m++;
	while(m){
		if(m%2)mul(b,a);
		mul(a,a);
		m/=2;
	}
	n/=2;
	for(int i=1;i<=n;i++){
		b[i][i+n]--;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cout<<b[i][j+n]<<' ';
		}
		cout<<'\n';
	}
	return 0;
}
2025/7/31 15:15
加载中...