AcWing矩阵乘 WA求调
  • 板块题目总版
  • 楼主luobotianle
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/1/26 22:11
  • 上次更新2024/1/27 10:06:38
查看原帖
AcWing矩阵乘 WA求调
1245780
luobotianle楼主2024/1/26 22:11

AcWing矩阵乘链接

想写个结构体的矩阵运算...然后WA了 :(
测试点还太大看不全
求调

#include<bits/stdc++.h>
#define mk make_pair
using namespace std;
const int N=105,mod=1e9+7;
int n;
struct Mat{
	long long n,m;
	long long a[N][N];
	bool is_square(){//Õý·½ÐξØÕóÅÐ¶Ï 
		return n==m;
	}
	void zero(){//ÇåÁã 
		n=0;
		m=0;
		memset(a,0,sizeof(a));
	}
	bool one(){// µ¥Î»¾ØÕó 
		if(!is_square()){
			printf("Error\n");
			return 0;
		}
		zero();
		for(int i=1;i<=n;i++)a[i][i]=1;
		return 1;
	}
	bool main_diagonal_set(auto x){//Ö÷¶Ô½ÇÏßÉèÖà 
		if(!is_square()){
			printf("Error\n");
			return 0;
		}
		for(int i=1;i<=n;i++)a[i][i]=x;
		return 1;
	}
	bool sub_diagonal_set(auto x){//¸±¶Ô½ÇÏßÉèÖà 
		if(!is_square()){
			printf("Error\n");
			return 0;
		}
		for(int i=1;i<=n;i++)a[i][n-i+1]=x;
		return 1;
	}
	void resize(auto x,auto y){//´óСÉèÖà 
		n=x;
		m=y;
	}
	Mat operator + (const Mat &A)const{//¼Ó 
		Mat ans;
		ans.zero();
		ans.resize(n,A.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				ans.a[i][j]=(a[i][j]+A.a[i][j])%mod;
			}
		}
		return ans;
	}
	Mat operator - (const Mat &A)const{//¼õ 
		Mat ans;
		ans.zero();
		ans.resize(n,A.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				ans.a[i][j]=(a[i][j]-A.a[i][j])%mod;
			}
		}
		return ans;
	}
	Mat operator * (const Mat &A)const{//³Ë 
		Mat ans;
		ans.zero();
		ans.resize(n,A.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=A.m;j++){
				for(int k=1;k<=m;k++){
					ans.a[i][j]=(a[i][k]*A.a[k][j]+ans.a[i][j])%mod;
				}
			}
		}
		return ans;
	}
	void input(auto x,auto y){//ÊäÈë 
		resize(x,y);
		for(int i=1;i<=x;i++){
			for(int j=1;j<=y;j++){
				scanf("%lld",&a[i][j]);
			}
		}
	}
	void output(){//Êä³ö 
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				printf("%lld ",a[i][j]);
			}
			printf("\n");
		}
	}
	void copy(Mat A){//¸´ÖÆ 
		zero();
		resize(A.n,A.m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				a[i][j]=A.a[i][j];
			}		
		}
	}
	void fill(auto x){//Ìî³ä 
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				a[i][j]=x;
			}
		}
	}
	bool fill(auto nstart,auto nend,auto mstart,auto mend,auto x){//¸ß¼¶Ìî³ä 
		if(nstart<1||mstart<1){
			printf("Underflow\n");
			return 0;
		}
		if(nend>n||mend>m){
			printf("Overflow\n");
			return 0;
		}
		for(int i=nstart;i<=nend;i++){
			for(int j=mstart;j<=mend;j++){
				a[i][j]=x;
			}
		}
		return 1;
	}
	bool quick_pow(auto x){//¿ìËÙÃÝ 
		if(!is_square()){
			printf("Error\n");
			return 0;
		}
		Mat tmp;
		tmp.copy(*this);
		Mat ans;
		ans.resize(n,m);
		ans.one();
		while(x){
			if(x&1){
				ans=ans*(*this);
			}
			(*this)=(*this)*(*this);
			x>>=1;
		}
		ans.output();
		copy(tmp);
		return 1;
	}
	bool change(auto x,auto y,auto w){//¸Ä±äijһֵ 
		if(x<1||y<1){
			printf("Underflow\n");
			return 0;
		}
		if(x>n||y>m){
			printf("Overflow\n");
			return 0;
		}
		a[x][y]=w;
		return 1;
	}
	void opposite(){//±äΪԭÀ´µÄ¸ºÊý (Ïà·´¾ØÕó?)
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				a[i][j]=-a[i][j];
			}
		}
	}
};
Mat x,y,ans;
int main(){
	long long n,m,p;
	cin>>n>>m;
	x.input(n,m);
	cin>>p;
	y.input(m,p);
	ans=x*y;
	ans.output();
	return 0;
}
2024/1/26 22:11
加载中...