Codeforces Global Round 2 Ramesses and Corner Inversion

33 篇文章 0 订阅

http://codeforces.com/contest/1119/problem/C

Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.

You are given two matrices AA and BB of size n×mn×m , each of which consists of 00 and 11 only. You can apply the following operation to the matrix AA arbitrary number of times: take any submatrix of the matrix AA that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain 00 , will be replaced by 11 , and all corners of the submatrix that contain 11 , will be replaced by 00 ). You have to answer whether you can obtain the matrix BB from the matrix AA .

An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow.

Ramesses don't want to perform these operations by himself, so he asks you to answer this question.

A submatrix of matrix MM is a matrix which consist of all elements which come from one of the rows with indices x1,x1+1,…,x2x1,x1+1,…,x2 of matrix MM and one of the columns with indices y1,y1+1,…,y2y1,y1+1,…,y2 of matrix MM , where x1,x2,y1,y2x1,x2,y1,y2 are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells (x1,y1)(x1,y1) , (x1,y2)(x1,y2) , (x2,y1)(x2,y1) , (x2,y2)(x2,y2) , where the cell (i,j)(i,j) denotes the cell on the intersection of the ii -th row and the jj -th column.

Input

The first line contains two integers nn and mm (1≤n,m≤5001≤n,m≤500 ) — the number of rows and the number of columns in matrices AA and BB .

Each of the next nn lines contain mm integers: the jj -th integer in the ii -th line is the jj -th element of the ii -th row of the matrix AA (0≤Aij≤10≤Aij≤1 ).

Each of the next nn lines contain mm integers: the jj -th integer in the ii -th line is the jj -th element of the ii -th row of the matrix BB (0≤Bij≤10≤Bij≤1 ).

Output

Print "Yes" (without quotes) if it is possible to transform the matrix AA to the matrix BB using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).

Examples

Input

3 3
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0

Output

Yes

Input

6 7
0 0 1 1 0 0 1
0 1 0 0 1 0 1
0 0 0 1 0 0 1
1 0 1 0 1 0 0
0 1 0 0 1 0 1
0 1 0 1 0 0 1
1 1 0 1 0 1 1
0 1 1 0 1 0 0
1 1 0 1 0 0 1
1 0 1 0 0 1 0
0 1 1 0 1 0 0
0 1 1 1 1 0 1

Output

Yes

Input

3 4
0 1 0 1
1 0 1 0
0 1 0 1
1 1 1 1
1 1 1 1
1 1 1 1

Output

No

题目大意: 给一个矩阵A和矩阵B,让你对矩阵A进行操作:任意选取矩阵A的一个子矩阵(行数和列数必须均大于等于2)将其四角的元素反转,问你能否经过任意次操作得到矩阵B。

思路:思维题目。确实没想到,看来思维还是有点狭隘。一个位置的数翻转两次就相等于没有翻转,那我们先不考虑第一行,对于剩余的行我们总可以使其翻转得到B,(即总是从第一行选取两个元素作为子矩阵的上面的两个角)此时对于每一列,当矩阵A与矩阵B不同的元素为偶数个的时候,才可以满足题意。因为这样第一行的元素才能满足题意,举个例子吧,其实也就两种情况:(1)第一行第一个元素不需要变换,此时若第一列A、B不同的元素有偶数个,那么必定能使A的第一列与B的第一列一致;若A、B不同的元素有奇数个,那么A第一行第一个元素多翻转了一次,不满足题意。(2)第一行第一个元素需要变化……(这里的分析同上 不写了)那么我们知道,如果A能转化成B,那么A、B每一行每一列不相同的元素个数必须都是偶数

#include<iostream>
#include<cstdio>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

int a[505][505];
int b[505][505];
int n,m;

int main()
{
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf("%d",&a[i][j]);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf("%d",&b[i][j]);
	int flag=0,cnt=0;
	for(int i=0;i<n;i++)//统计行
	{
		cnt=0;
		for(int j=0;j<m;j++)
			if(a[i][j]!=b[i][j])
				++cnt;
		if(cnt&1)
		{
			flag=1;
			break;
		}
	}
	if(flag)
		printf("No\n");
	else
	{
		for(int i=0;i<m;i++)//统计列
		{
			cnt=0;
			for(int j=0;j<n;j++)
				if(a[j][i]!=b[j][i])
					++cnt;
			if(cnt&1)
			{
				flag=1;
				break;
			}
		}
		if(flag)
			printf("No\n");
		else
			printf("Yes\n");
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值