Codeforces Round #443 (Div. 2) C: Short Program - 位运算
题目大意:
输入给出一串位运算,输出一个步数小于等于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 - 位运算的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算
D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...
- Codeforces Round #879 (Div. 2) C. Short Program
题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...
- Codeforces Round #499 (Div. 2) F. Mars rover_dfs_位运算
题解: 首先,我们可以用 dfsdfsdfs 在 O(n)O(n)O(n) 的时间复杂度求出初始状态每个点的权值. 不难发现,一个叶子节点权值的取反会导致根节点的权值取反当且仅当从该叶子节点到根节点这 ...
- Codeforces Round #626 (Div. 2) D. Present(位运算)
题意: 求n个数中两两和的异或. 思路: 逐位考虑,第k位只需考虑0~k-1位,可通过&(2k+1-1)得到一组新数. 将新数排序,当两数和在[2k,2k+1)和[2k+1+2k,2k+2)之 ...
- Codeforces Round #443 (Div. 2) 【A、B、C、D】
Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...
- Codeforces Round #443 (Div. 2)
C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...
- Codeforces Round #443 (Div. 2) C 位运算
C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- String.Empty,NULL和""的区别
String.Empty,NULL和""的区别 string.Empty就相当于"" 一般用于字符串的初始化 比如: string a; Console.Wri ...
- WebClient HttpWebRequest从网页中获取请求数据
WebClient HttpWebRequest //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlAddress) ...
- 标准模板库 STL 使用之 —— vector 使用 tricks
1. 从已有 vector(或数组)中复制 vector<int> a{....}; int an = a.size(); int half = an/2; vector<int&g ...
- elementUI upload 对图片的宽高做校验
很开心今天中午没有吃饭!原因是一直没有解决掉一个小问题,于是一直试错,最后看了下源码才有了点头绪,历时四五个小时才解决掉,有点怀疑自己的能力了,所以写下此文,记录一下今天的囧况!一般情况下遇到问题,自 ...
- cmake的使用笔记
一. cmake使用网上还蛮多的.可是当你自己去用的时候,才觉得,都不是你想要的. 自己做个记录,用法全是基本用法. 二. 我在里面直接写注释说明了,直接贴了. 1. #cmake #1. # cma ...
- “-bash: !”: event not found"、echo > sudo permission denied
1. "-bash: !": event not found" 比如当我们在 linux 命令行输入echo "Reboot your instance!&qu ...
- pcb过孔盖油
pcb的过孔应该盖油,,这样,两个距离比较紧的过孔就不会在焊接的时候短路了,尤其是手工焊接小件的时候.
- 【BZOJ 3172】单词
[链接]h在这里写链接 [题意] 给你n个单词; 这n个单词组成了一篇文章; 问你每个单词在这篇文章中出现了多少次. 其中每个单词之间用一个逗号隔开->组成一篇文 ...
- Spring 使用Cache(转)
从3.1开始Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事物管理的支持.Spring Cache是作用在方法上的,其核心思想是:当我们在调用一个缓存方法时会把该方法参数 ...
- [NodeJS] Use Now alias for custom sub-domains
Now is a great way to deploy your node application, but the generated URLs aren't memorable or easil ...