矩阵

矩阵本体

struct Mat{
	int mat[NUM][NUM]={};
	int h,w;
	Mat operator + (const Mat oth){
		Mat ans;ans.resize(h,w);
		for(int i=1;i<=h;++i)
		for(int j=1;j<=w;++j)
			ans.mat[i][j]=mat[i][j]+oth.mat[i][j];
		return ans;
	} 
	Mat operator * (const Mat oth){
		Mat ans;ans.h=h,ans.w=oth.w;
		for(int i=1;i<=ans.h;++i)
		for(int k=1;k<=w;++k)
		for(int j=1;j<=ans.w;++j)
			(ans.mat[i][j]+=mat[i][k]*oth.mat[k][j])%=mod;
		return ans;
	}
	void resize(int n,int m){h=n,w=m;}
	void resize(Mat a){h=a.h,w=a.w;}
	void setone(){
		for(int i=1;i<=h;++i) mat[i][i]=1;
	}
	void print(){
		for(int i=1;i<=h;++i){
			for(int j=1;j<=w;++j)
			cout<<mat[i][j]<<' ';
			cout<<'\n';
		}
	}
	void print_line(int n){
		for(int j=1;j<=w;++j) cout<<mat[n][j]<<' ';
	}
}A;

矩阵快速幂

Mat ksm(Mat a,int b){
	Mat ans;ans.resize(a);ans.setone();
	for(;b;b>>=1,a=a*a)
		if(b&1) ans=ans*a;
	return ans;
}

矩阵套矩阵

不知何意未

struct MAT{
	Mat mat[NUM][NUM];
	int h,w,hin,win;
	MAT operator * (const MAT oth){
		MAT ans;ans.h=h,ans.w=oth.w;
		for(int i=1;i<=ans.h;++i)
		for(int k=1;k<=w;++k)
		for(int j=1;j<=ans.w;++j)
			ans.mat[i][j].resize(hin,win),
			ans.mat[i][j]=ans.mat[i][j]+mat[i][j]*oth.mat[i][j];
		return ans;
	}
	void resize_out(int n,int m){
		h=n,w=m;
	}
	void resize_in(int n,int m){
		hin=n,win=m;
		for(int i=1;i<=h;++i)
		for(int j=1;j<=w;++j)
		mat[i][j].resize(n,m);
	}
	void setone(){
		for(int i=1;i<=h;++i) mat[i][i].setone();
	}
	void print(){
		for(int i=1;i<=h;++i){
			for(int k=1;k<=mat[1][1].h;++k){
				for(int j=1;j<=w;++j)
				mat[i][j].print_line(k),cout<<' ';
				cout<<'\n';
			}
			cout<<'\n';
		}
	}
}A;
posted @ 2026-06-27 19:09  LZYXT  阅读(3)  评论(0)    收藏  举报