传送门

题目大意:

输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一。

题目分析:

英文题的理解真的是各种误差,从头到尾都以为解是唯一的。

根据位运算的性质可以知道:

一连串的位运算最终都可以用三个位运算代替(&|^)。

那么仅需对每一位的情况进行讨论,某一位:

  • 必须变为1 (|1)
  • 必须变为0 (&0)
  • 必须01颠倒(^1)

    最后输出3种位运算即可(输出三种最稳妥)。

code

#include<bits/stdc++.h>
using namespace std;
int x, y, n; int main(){
//freopen("h.in", "r", stdin);
scanf("%d", &n);
x = 0, y = (1<<10)-1;
for(int i = 1; i <= n; i++){
char opt[5];
int v;
scanf("%s %d", opt + 1, &v);
if(opt[1] == '|'){
x |= v;
y |= v;
}
else if(opt[1] == '&'){
x &= v;
y &= v;
}
else if(opt[1] == '^'){
x ^= v;
y ^= v;
}
}
int orr, andd, xorr;
orr = andd = xorr = 0;
for(int i = 0; i < 10; i++){
int t = 1 << i;
if(x & t){
if(y & t) orr |= t;
else xorr |= t;
andd |= t;
}
else{
if(y & t)
andd |= t;
}
}
printf("3\n");
printf("& %d\n", andd);
printf("| %d\n", orr);
printf("^ %d\n", xorr);
return 0;
}

Codeforces Round #443 (Div. 2) C: Short Program - 位运算的更多相关文章

  1. Codeforces Round #443 (Div. 1) A. Short Program

    A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...

  2. Codeforces Round #443 (Div. 2) C. Short Program

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  4. Codeforces Round #879 (Div. 2) C. Short Program

    题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...

  5. Codeforces Round #499 (Div. 2) F. Mars rover_dfs_位运算

    题解: 首先,我们可以用 dfsdfsdfs 在 O(n)O(n)O(n) 的时间复杂度求出初始状态每个点的权值. 不难发现,一个叶子节点权值的取反会导致根节点的权值取反当且仅当从该叶子节点到根节点这 ...

  6. Codeforces Round #626 (Div. 2) D. Present(位运算)

    题意: 求n个数中两两和的异或. 思路: 逐位考虑,第k位只需考虑0~k-1位,可通过&(2k+1-1)得到一组新数. 将新数排序,当两数和在[2k,2k+1)和[2k+1+2k,2k+2)之 ...

  7. Codeforces Round #443 (Div. 2) 【A、B、C、D】

    Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...

  8. Codeforces Round #443 (Div. 2)

    C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...

  9. Codeforces Round #443 (Div. 2) C 位运算

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

随机推荐

  1. 使用Docker来运行WebApp

    原文:使用Docker来运行WebApp (作者:陈玓玏) 1.加载镜像到容器并运行webapp脚本 先进入管理员模式: su root 然后使用已有的webapp镜像来练习在docker上运行web ...

  2. MyCat中间件:读写分离(转)

    利用MyCat中间件实现读写分离 需要两步: 1.搭建MySQL主从复制环境 2.配置MyCat读写分离策略 一.搭建MySQL主从环境 参考上一篇博文:MySQL系列之七:主从复制 二.配置MyCa ...

  3. ajax上传进度条

    <script type="text/javascript"> function register(){ var frm = document.getElementBy ...

  4. php字符串函数分类总结

    php字符串函数分类总结 一.总结 explode  str_split  str_word_count  strtolower 二.php字符串函数分类总结 php内置了98个字符串函数(除了基于正 ...

  5. 算法中的优化问题(optimization problem)

    和多数算法不同的是,有些问题的答案不只一个,而是需要在多个答案中,按照一定标准选出"最佳"答案,这类问题就统称为"优化问题"(optimization prob ...

  6. Altium Designer中DRC错误分析

  7. 基于Qt Assistant制作软件帮助文档

    Qt Assistant是Qt自带的一款可定制.可重新发行的帮助文件浏览器.Qt Assistant支持HTML文件,用户可以利用其定制自己的功能强大的帮助文档浏览器.关于Qt Assistant定制 ...

  8. vue 用 :style动态修改带中划线的样式属性

    今天在项目中遇到要用:style动态设置margin-top值,直接写发现报错.后来改成驼峰就成功了,记录一下 错误示范: <div class="testLeft ulData&qu ...

  9. 结合Wireshark捕获分组深入理解TCP/IP协议栈之HTTP协议

    摘要:     本文简单介绍了Web应用层协议理论知识,详细讲述了HTTP请求报文和响应报文各个字段含义,并从Wireshark俘获分组中选取HTTP相关报文进行分析. 一.概述     Web的应用 ...

  10. sql海量数据优化

    1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设 ...