题目链接:http://codeforces.com/contest/1151/problem/B
题目大意:
给定一个n*m的矩阵,里面存放的是自然数,要求在每一行中选一个数,把他们异或起来后结果大于0,如果存在一种方案,就把每行所选数的列号输出。
分析:
我们只关注这些数的第i位二进制位,如果存在某一行比如说第k行,这一行中有第i位二进制位为1的数,也有第i位二进制位为0的数,那么可以说,这一行是决定性的行,无论其他行怎么选择,这一行只要根据其他行异或的结果,变通地选择第i位二进制位为0或1的数,必然能使最终结果大于0。
代码如下:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ; int n, m;
int matrix[][];
// rowOR[i]第i行全或的值
// rowAND[i]第i行全与的值
int rowOR[], rowAND[];
// rowXOR[i]的二进制位如果为1,表示第i行在这一位上有0或1两种选择,否则只有一种
int rowXOR[];
// availableBits的二进制位如果为1,表示存在一种选择策略,异或完后这一位二进制位不为0
int availableBits;
int ans[];
int ansXOR; int main(){
INIT();
cin >> n >> m;
For(i, , n) {
rowAND[i] = ( << ) - ;
For(j, , m) {
cin >> matrix[i][j];
rowOR[i] |= matrix[i][j];
rowAND[i] &= matrix[i][j];
}
rowXOR[i] = rowOR[i] ^ rowAND[i];
availableBits |= rowXOR[i];
} int targetBit = LOWBIT(availableBits); bool flag = true;
int tmp; For(i, , n) {
if((rowXOR[i] & targetBit) != && flag) {
tmp = i; // tmp保存决定性的行
flag = false;
continue;
}
ans[i] = ;// 其他行无所谓,统一选择行首元素
ansXOR ^= matrix[i][];
} if(!flag) {
For(j, , m) {
if((ansXOR ^ matrix[tmp][j]) != ) {
ansXOR ^= matrix[tmp][j];
ans[tmp] = j;
break;
}
}
} if(ansXOR) {
cout << "TAK" << endl;
For(i, , n) cout << ans[i] << " ";
cout << endl;
}
else cout << "NIE" << endl;
return ;
}