【CodeForces 1151B --- Dima and a Bad XOR】DP+位运算

【CodeForces 1151B --- Dima and a Bad XOR】DP+位运算

题目来源:点击进入【CodeForces 1151B — Dima and a Bad XOR】

Description

Student Dima from Kremland has a matrix a of size 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 c1,c2,…,cn (1≤cj≤m) so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds, where ai,j is the matrix element from the i-th row and the j-th column.

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

Input

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

Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. ai,j (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 integers c1,c2,…cn (1≤cj≤m), so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.

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

Sample Input

3 2
0 0
0 0
0 0

Sample Output

NIE

解题思路

根据题意我们发现arr[]数组的值范围为0-1023,那么就代表着不管怎么异或都不可能为负数。
所以我们只需要在意异或后是否等于0即可。
那么我们就将每一行的第一个拿来异或,然后判断是否等于0.
如果不等于零那就说明满足题意。
如果等于零:我们可以在之前判断是否存在一行中有不同的值,第一个值异或后为0,那么异或另一个值肯定就不为0.所以我们可以之前通过map记录是否存在不同的值。并且记录那个所在的行和列。

AC代码:

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int MAXN = 505;
int arr[MAXN][MAXN];
map<int,int> mp[MAXN];

int main()
{
    SIS;
    int n,m,res=0,x=-1,y;
    cin >> n >> m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >> arr[i][j];
            if(!j) res^=arr[i][j];
            mp[i][arr[i][j]]++;
            if(mp[i][arr[i][j]]==1 && j!=0) x=i,y=j+1;
        }
    }
    if(res || x!=-1)
    {
        cout << "TAK" << endl;
        if(res)
        {
            for(int i=0;i<n;i++) cout << 1 << ' ';
            cout << endl;   
        }
        else
        {
            for(int i=0;i<x;i++) cout << 1 << ' ';
            cout << y << ' ';
            for(int i=x+1;i<n;i++) cout << 1 << ' ';
            cout << endl;
        }
    }
    else cout << "NIE" << endl;
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104103963