CodeForces 1151B Dima and a Bad XOR
题目链接: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的更多相关文章
- Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告
题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质 交换律 x1^x2^x3==x3^x2 ...
- Codeforces Round #553 B. Dima and a Bad XOR
题面: 传送门 题目描述: 题意很简单:在一个N*M的矩阵中(N行M列),问是否可以:每行选一个整数,使他们的异或和大于0.如果不可以,输出"NIE":如果可以,输出"T ...
- 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 ...
- 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. 这题 ...
- CodeForces 584D Dima and Lisa
1e9 以内的判断一个数是否是素数,可以直接朴素的暴力. 这倒题除了考虑1e9以内的素数的判断,还有一个歌德巴赫猜想:任意一个奇数都可一分解为三个素数的和. 第三个结论:素数是密集的,1e9以内, ...
- 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 ...
- 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 ...
- Codeforces Little Dima and Equation 数学题解
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces B. Dima and Text Messages 解题报告
题目链接:http://codeforces.com/problemset/problem/358/B 题目意思:给出n个单词(假设为word1,word2.word3...wordn)和一句test ...
随机推荐
- Uncaught SyntaxError: Invalid shorthand property initializer报错
出现这个错误一定是:把":"写成了"="号
- Java 学习笔记 IO流与File操作
可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...
- Java 学习笔记 使用synchronized实现生产者消费者模式
说明 Object.wait()使当前的线程进入到等待状态(进入到等待队列) Object.notifyAll() 唤醒等待中的全部线程 Object.notify() 随机唤醒一个线程 代码 con ...
- Elasticsearch系列(3):Elasticsearch操作入门
创建Index 新建Index,可以直接向Elastic服务器发送PUT请求,比如下面的命令创建了一个名为:logdb的Index. [root@elsearchserver ~]# curl -X ...
- PHP实现微信随机红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- HTML之body标签中的相关标签
一 字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub& ...
- java新知识系列 一
内联函数: 所谓内联函数就是指函数在被调用的地方直接展开,编译器在调用时不用像一般函数那样,参数压栈,返回时参数出栈以及资源释放等,这样提高了程序执行速度. 对应Java语言中也有一个关键字final ...
- SpringBoot实现全文搜索
• 全文搜索 • solr安装 • solr中文分词 • solr数据库导入 • solr数据查询 • solrj接口调用 1:
- Go-Ethereum 1.7.2 结合 Mist 0.9.2 实现代币智能合约的实例
目录 目录 1.什么是 Mist 2.Mist 在哪里下载? 3.Mist 有哪些依赖? 4.如何安装 Mist? 4.1.安装 Mist 依赖工具包 4.2.安装 Mist 4.3.启动 Mist, ...
- Visual Studio 2017的安装与使用
Visual Studio 2017的安装与使用 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 下载Visual Studio 2017 在官网 ...