题目链接: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 ;
}

CodeForces 1151B Dima and a Bad XOR的更多相关文章

  1. Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告

    题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质  交换律 x1^x2^x3==x3^x2 ...

  2. Codeforces Round #553 B. Dima and a Bad XOR

    题面: 传送门 题目描述: 题意很简单:在一个N*M的矩阵中(N行M列),问是否可以:每行选一个整数,使他们的异或和大于0.如果不可以,输出"NIE":如果可以,输出"T ...

  3. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  4. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  5. CodeForces 584D Dima and Lisa

    1e9 以内的判断一个数是否是素数,可以直接朴素的暴力.   这倒题除了考虑1e9以内的素数的判断,还有一个歌德巴赫猜想:任意一个奇数都可一分解为三个素数的和. 第三个结论:素数是密集的,1e9以内, ...

  6. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

    任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  7. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  8. Codeforces Little Dima and Equation 数学题解

    B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. codeforces B. Dima and Text Messages 解题报告

    题目链接:http://codeforces.com/problemset/problem/358/B 题目意思:给出n个单词(假设为word1,word2.word3...wordn)和一句test ...

随机推荐

  1. C# 绘制Word形状——基本形状、组合形状

    一.序言 在Office Word中,支持在Word文档中插入类型非常丰富的形状,包括线条.矩形.基本形状(诸如圆形.多边形.星形.括号.笑脸等等图形).箭头形状.公式形状.流程图.旗帜图形.标注图形 ...

  2. python学习笔记(七)、异常

    在实际开发中,往往不会出现一个系统编写下来,没有出现任何一个bug.为处理这种会出现错误的地方,如除数为零的除法,python提供了非常强大的机制————异常处理机制. 1 异常 python使用异常 ...

  3. 2018.12/6 js键盘事件 DOM:0级2级

    DOM0级事件元素绑定多个click最后只执行最后一个click. DOM2级事件元素绑定多个click,都要执行 注意当绑定的多个事件名,函数名,事件发生阶段三者完全一样时,才执行最后一个 div. ...

  4. Django学习之五:Django 之 注意事项及汇总

    目录 Django 之 注意事项及汇总 全局 settings model模块-模型模块 URLs模块 Templates System 模版模块 View/HttpRequest/HttpRespo ...

  5. 【代码笔记】Web-CSS-CSS 链接(link)

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  6. Dynamics 365的审核日志分区删除超时报错怎么办?

    摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复296或者20190112可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...

  7. asp.net/wingtip/UI和导航

    ASP.NET Web窗体可以让web应用创建动态内容.静态网页的知识和HTML.CSS一模一样,区别在于ASP.NET网页包括了ASP.NET可以识别并处理的服务器端的内容.对于静态页面(.html ...

  8. python(day16)内置函数,匿名函数

    # add = lambda x,y:x+y # print(add(1,2)) # dic={'k1':10,'k2':100,'k3':30} # def func(key): # return ...

  9. LeetCode算法题-Max Consecutive Ones(Java实现)

    这是悦乐书的第242次更新,第255篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第109题(顺位题号是485).给定二进制数组,找到此数组中连续1的最大数量.例如: 输 ...

  10. const int *p;int *const p;区别及用法

    在单片机程序设计中,我们经常会用到const这个关键字,在有些单片机的编译器中可能会是code(比如51系列单片机),但我们在学习C语言的时候,首先还是先学到的const.我们知道,const关键字的 ...