【考虑周全+数学变形】【11月赛】Is it a fantastic matrix?

9 篇文章 0 订阅
3 篇文章 0 订阅

Is it a fantastic matrix?

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2

Problem Description

Given an n*m matrix, you are asked to judge if it's fantastic. The rule is: firstly, choose min (n, m) numbers from it, meanwhile you should make sure any two numbers you select will not at the same row or same column. Of course, you may have many different ways to get the numbers. For every way, if the sum of all selected numbers is always same, you may say the matrix is fantastic.

Input

The first line contains an integer T, stands for the number of test cases. (1<=T<=100)
T cases follow, for every case:
The first line contains two integers n and m. (1 <= n, m <= 50)
Then n lines follows, each line contains m integers. Every number in the matrix will between -100000 and 100000.

Output

For every case, if it is a fantastic matrix, output “YES” in one line, otherwise output “NO”.

Sample Input

3
2 2
1 3
2 4
2 3
1 1 1
1 1 1
2 1
2
3

Sample Output

YES
YES
NO

Source

hujie 测试专用(2)

这个题当初就思考了很久 可惜还是没有考虑 周全

当n<m的时候 每一行都必须相同
1 1 1 1
2 2 2 2
3 3 3 3
如果不相同显然反例可证明
n>m 时候同理


当时只考虑了n==m的情况
得到了等式 a[k][jk]+a[t][jt]=a[k][jt]+a[t][jk]; (利用行列式的方式描述一个元素的位置 a[1][j1])
当k=1 ,jk=1 的时候  a[1][1]+a[t][jt]=a[1][jt]+a[t][1];
同时可以写出另外3个
a[k][jk]+a[1][1]=a[k][1]+a[1][jk]
a[1][jk]+a[t][1]=a[1][1]+a[t][jk]
a[k][1]+a[1][jt]=a[k][jt]+a[1][1]
累加起来即为  a[k][jk]+a[t][jt]=a[k][jt]+a[t][jk]

所以我们只需判断 a[x][y]+a[1][1]==a[x][1]+a[1][y]即可
代码如下:
#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313   
using namespace std;
int MAP[60][60];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int OK=1;
		int m,n;
		cin>>n>>m;
		if(n>m) //懒得写
		if(n<m) //懒得写 
		for(int i=1;i<=n;i++)
		 for(int j=1;j<=m;j++)
			scanf("%d",&MAP[i][j]);
		if(n==1||m==1) { printf("NO\n");continue;}
				for(int i=1;i<=n;i++)
		    	 for(int j=1;j<=m;j++)
		    	 if(MAP[i][j]+MAP[1][1]!=MAP[1][j]+MAP[i][1]) OK=0; 
		if(OK) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}	
}
  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值