当前位置: 首页 > news >正文

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。

代码如下:

  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49  
 50 typedef long long LL;
 51 typedef unsigned long long uLL;
 52 typedef pair< double, double > PDD;
 53 typedef pair< int, int > PII;
 54 typedef set< int > SI;
 55 typedef vector< int > VI;
 56 typedef map< int, int > MII;
 57 const double EPS = 1e-10;
 58 const int inf = 1e9 + 9;
 59 const LL mod = 1e9 + 7;
 60 const int maxN = 2e5 + 7;
 61 const LL ONE = 1;
 62 
 63 int n, m; 
 64 int matrix[507][507];
 65 // rowOR[i]第i行全或的值
 66 // rowAND[i]第i行全与的值
 67 int rowOR[507], rowAND[507];
 68 // rowXOR[i]的二进制位如果为1,表示第i行在这一位上有0或1两种选择,否则只有一种 
 69 int rowXOR[507];
 70 // availableBits的二进制位如果为1,表示存在一种选择策略,异或完后这一位二进制位不为0 
 71 int availableBits;
 72 int ans[507];
 73 int ansXOR;
 74 
 75 int main(){
 76     INIT();
 77     cin >> n >> m;
 78     For(i, 1, n) {
 79         rowAND[i] = (1 << 11) - 1;
 80         For(j, 1, m) {
 81             cin >> matrix[i][j];
 82             rowOR[i] |= matrix[i][j];
 83             rowAND[i] &= matrix[i][j];
 84         }
 85         rowXOR[i] = rowOR[i] ^ rowAND[i];
 86         availableBits |= rowXOR[i];
 87     }
 88     
 89     int targetBit = LOWBIT(availableBits);
 90     
 91     bool flag = true;
 92     int tmp;
 93     
 94     For(i, 1, n) {
 95         if((rowXOR[i] & targetBit) != 0 && flag) {
 96             tmp = i; // tmp保存决定性的行 
 97             flag = false;
 98             continue;
 99         }
100         ans[i] = 1;// 其他行无所谓,统一选择行首元素 
101         ansXOR ^= matrix[i][1];
102     }
103     
104     if(!flag) {
105         For(j, 1, m) {
106             if((ansXOR ^ matrix[tmp][j]) != 0) {
107                 ansXOR ^= matrix[tmp][j];
108                 ans[tmp] = j;
109                 break;
110             }
111         }
112     }
113     
114     if(ansXOR) {
115         cout << "TAK" << endl;
116         For(i, 1, n) cout << ans[i] << " ";
117         cout << endl;
118     }
119     else cout << "NIE" << endl;
120     return 0;
121 }
View Code

 

转载于:https://www.cnblogs.com/zaq19970105/p/10748749.html

相关文章:

  • unity Stopwatch
  • unity 烘焙参数的设置
  • JS 设计模式五 -- 命令模式
  • CommandBuffer实现Distort屏幕扭曲效果
  • Anti-aliasing and Continuity with Trapezoidal Shadow Maps
  • tarjan----强连通分量
  • Trapezoidal Shadow Maps (TSM) - Recipe
  • 十天冲刺-03
  • Perspective Shadow Maps: Care and Feeding
  • 网络基础 08_NAT
  • C# AttributeUsage
  • vue组件之间的传值方式
  • C# Attribute
  • linux 网络虚拟化: network namespace 简介
  • TBDR
  • FineReport中如何实现自动滚屏效果
  • JS函数式编程 数组部分风格 ES6版
  • js作用域和this的理解
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • passportjs 源码分析
  • Python学习笔记 字符串拼接
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • windows下如何用phpstorm同步测试服务器
  • 彻底搞懂浏览器Event-loop
  • 成为一名优秀的Developer的书单
  • 简析gRPC client 连接管理
  • 将回调地狱按在地上摩擦的Promise
  • 区块链共识机制优缺点对比都是什么
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #NOIP 2014# day.1 T2 联合权值
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (floyd+补集) poj 3275
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (一)kafka实战——kafka源码编译启动
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (转)Unity3DUnity3D在android下调试
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .NET构架之我见
  • .NET微信公众号开发-2.0创建自定义菜单
  • .NET应用架构设计:原则、模式与实践 目录预览
  • @Controller和@RestController的区别?
  • @WebService和@WebMethod注解的用法
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [ANT] 项目中应用ANT
  • [BUG] Authentication Error
  • [BZOJ 3531][Sdoi2014]旅行(树链剖分+线段树)
  • [C++]高精度 bign (重载运算符版本)