题目链接: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. Mysql中的WITH ROLLUP用法

    1.WITH ROLLUP:在group分组字段的基础上再进行统计数据. 例子:首先在name字段上进行分组,然后在分组的基础上进行某些字段统计,表结构如下: CREATE TABLE `test` ...

  2. 20, CSS 定义选择器

    1. ID 与类 2. 层叠 3. 分组 4. 继承 5. 上下文选择器 6. 子类选择器 7. 其他选择器 8. 结构与注释 20.1 ID 与类 选择器是用于控制页面设计的样式.即 ID 选择器何 ...

  3. Linux系统使用

    linux(操作系统的内核) 浏览器功能:(内核的解释) 各个浏览器 实现的方式不一样 呈现内容 //解析内容和样式 用—webkit— (内核)解析 实现交互逻辑 v8 引擎 (内核) 实现 =&g ...

  4. arcgis api 3.x for js 入门开发系列四地图查询(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  5. 工程造价数据服务云平台(造价BIM)

    为响应招标人的<ZQH工程造价数据平台>的技术邀约,特作以下陈述. 经过多次沟通和对招标文件的理解,招标人通过软件平台建立和使用人员库.项目库.材料设备价格库.数据库等四库的真实需求,本着 ...

  6. Android为TV端助力listview 非常重要的几个属性

    首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false Android:stackFromBottom="true" ...

  7. 预置第三方apk到MTK项目相关问题总结

    目前5.0之后项目预置方式通用步骤为: 建立apk文件夹;  置目标apk到该文件夹下;   解压缩apk查看是否包含lib/文件夹(apk项目是否包含lib库文件);  在该文件夹下编写Androi ...

  8. Spring容器

    1.Spring简介: a)Spring春天 b)官网:https://spring.io c)设计理念:轮子理念,不要重复创造轮子: d)Spring可以被理解为一个容器,用于管理其他的框架: e) ...

  9. MySQL 基础知识梳理学习(一)----系统数据库

    information_schema 此数据库是MySQL数据库自带的,主要存储数据库的元数据,保存了关于MySQL服务器维护的所有其他数据库的信息,如数据库名.数据库表.表列的数据类型及访问权限等. ...

  10. Proxmox VE登陆的时候提示没有有效的订阅You do not have a valid subscription for this server. Please visit www.proxmox.com to get a list of available options.

    问题描述: 用的是免费版的,所以每次都提示这个没有有效的订阅挺烦的 解决方法: 修改文件/usr/share/javascript/proxmox-widget-toolkit/proxmoxlib. ...