为什么样例都没过?
查看原帖
为什么样例都没过?
738882
OIer_dcy__AK_IOI楼主2024/1/6 18:32
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7; 
struct Mat{
	int r,c;
	long long a[110][110];
	Mat(int rr=0,int cc=0){
		r=rr,c=cc;
		if(c==0) c=r;
		memset(a,0,sizeof a);
	}
	void unit(){
		memset(a,0,sizeof a);
		for(int i=1;i<=r;i++){
			a[i][i]=1;
		}
	}
	Mat operator+(const Mat &tmp)const{
		Mat ans=*this;
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				ans.a[i][j]+=tmp.a[i][j];
			}
		}
		return ans;
	}
	Mat operator-(const Mat &tmp)const{
		Mat ans=*this;
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				ans.a[i][j]-=tmp.a[i][j];
			}
		}
		return ans;
	}
	Mat operator*(const Mat &tmp)const{
		int n=r;
		int m=tmp.c;
		Mat ans(n,m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				for(int k=1;k<=c;k++){
					ans.a[i][j]=(ans.a[i][j]+a[i][j]*tmp.a[i][j]%mod)%mod;
				}
			}
		}
		return ans;
	}
	Mat ksm(long long b){
		Mat ans(r,c),aa;
		aa=*this,ans.unit();
		while(b){
			if(b&1){
				ans=ans*aa;
			}
			aa=aa*aa;
			b>>=1;
		}
		return ans;
	}
	void print(){
		for(int i=1;i<=r;i++){
			for(int j=1;j<=c;j++){
				printf("%lld ",a[i][j]);
			}
			printf("\n");
		}
	}
};
int n; 
Mat A,fast;
int main(){
	long long k;
	scanf("%d%lld",&n,&k);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%lld",&A.a[i][j]);
		}
	}
	A.r=n;
	fast=A.ksm(k);
	fast.print();
	return 0;
}
2024/1/6 18:32
加载中...