传送门

题目大意:

输入给出一串位运算,输出一个步数小于等于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. TextView-shadow 阴影实现

    直接上代码 1)实现普通效果 <TextView android:layout_width="match_parent" android:layout_height=&quo ...

  2. Java Web学习总结(3)——Servlet详解

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  3. 删除dataGridview中选中的一行或多行

    一.实现的功能:可以删除一行或者多行数据,并在删除前提醒是否确定进行删除! DialogResult RSS = MessageBox.Show(this,"确定要删除选中行数据码?&quo ...

  4. 【Educational Codeforces Round 31 B】Japanese Crosswords Strike Back

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 所有数字的和加上n-1,如果为x则唯一,否则不唯一 [代码] #include <bits/stdc++.h> usin ...

  5. amazeui页面分析3

    amazeui页面分析3 一.总结 1. 本质是list列表,是ul套li的形式,只不过li里面是图片 <li class="am-g am-list-item-desced am-l ...

  6. HDU 1212 Big Number 大数模小数

    http://acm.hdu.edu.cn/showproblem.php?pid=1212 题目大意: 给你一个长度不超过1000的大数A,还有一个不超过100000的B,让你快速求A % B. 什 ...

  7. mysql数据库各存储引擎比較

    mysql数据库差别于其它数据库的最重要的一个特点是其插件式的表存储引擎,存储引擎是基于表的.而不是数据库 InnoDB存储引擎:        支持事务,其设计目标主要面向在线事务处理(OLTP)的 ...

  8. [Nuxt] Add CSS Libraries to Nuxt

    You can easily add CSS libraries to Nuxt using yarn or npm to install them, then simply adding them ...

  9. [Node] Run Local DevDependencies from the Command Line with npx

    In the past, you've needed to either write a package.json script or use the node_modules/.bin direct ...

  10. 5DXTPlayer串口调试小结

    小结 过程总是艰难. 首先是没有准备好. 没有安装vs2012,安装的时候,出现各种状况,因为先安装的2013高版本,造成12安装不正确,程序编译出问题.没有办法,只好卸载vs2012,2013及其各 ...