Codeforces1151B-Dima and a Bad XOR(贪心构造)

B. Dima and a Bad XOR

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Student Dima from Kremland has a matrix a
a
of size n×m
n×m
filled with non-negative integers.

He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!

Formally, he wants to choose an integers sequence c 1 ,c 2 ,…,c n
c1,c2,…,cn
(1≤c j ≤m
1≤cj≤m
) so that the inequality a 1,c 1 ⊕a 2,c 2 ⊕…⊕a n,c n >0
a1,c1⊕a2,c2⊕…⊕an,cn>0
holds, where a i,j
ai,j
is the matrix element from the i
i
-th row and the j
j
-th column.

Here x⊕y
x⊕y
denotes the bitwise XOR operation of integers x
x
and y
y
.

Input

The first line contains two integers n
n
and m
m
(1≤n,m≤500
1≤n,m≤500
) — the number of rows and the number of columns in the matrix a
a
.

Each of the next n
n
lines contains m
m
integers: the j
j
-th integer in the i
i
-th line is the j
j
-th element of the i
i
-th row of the matrix a
a
, i.e. a i,j
ai,j
(0≤a i,j ≤1023
0≤ai,j≤1023
).

Output

If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print “NIE”.

Otherwise print “TAK” in the first line, in the next line print n
n
integers c 1 ,c 2 ,…c n
c1,c2,…cn
(1≤c j ≤m
1≤cj≤m
), so that the inequality a 1,c 1 ⊕a 2,c 2 ⊕…⊕a n,c n >0
a1,c1⊕a2,c2⊕…⊕an,cn>0
holds.

If there is more than one possible answer, you may output any.

题解: 这个题目其实想通了很简单,作为cf的b题肯定不会很难,我们只要构造出任意一种异或和大于1就行,我们知道,如果第一列的值异或和>1,就直接全输入1即可,如果等于0,我们只要在任意一行中找到一个不等于这一行的第一个元素就行,因为其他数异或这行第一个数为0,那其他数异或这个不等于第一个的数肯定大于1。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	int a[505][505];
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans^=a[i][1];
	}
	if(ans)
	{
		puts("TAK");
		for(int i=1;i<=n;i++)
		{
			printf("1 ");
		}
		cout<<endl;
		return 0;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=2;j<=m;j++)
		{
			if(a[i][j]!=a[i][1])
			{
				puts("TAK");
				for(int k=1;k<=n;k++)
				{
					if(k!=i)
					printf("1 ");
					else
					printf("%d ",j);
				}
				cout<<endl;
				return 0;
			}
		}
	}
	puts("NIE");
 } 
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值