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");
}