原题链接:http://codeforces.com/problemset/problem/878/A

题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步。

思路:我们可以对二进制每一位上的1, 0, 进行讨论,

如果n次操作后1 -->1, 0 --> 1, 说明这一位要用或操作(or 1)

类似的,1 -->0, 0 -->1, 说明这一位要用异或操作(xor 1)

         1 -->0, 0 -->0, 说明这一位要用与操作(and 0)

1 -->1, 0 -->0,前后不变,可以不用进行操作。

其中,and 0 操作可以用 or 1 xor 1代替, 那么最后化简后的操作次数不会超过 2 次。

AC代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<string>
using namespace std;
typedef long long LL;
int XOR, OR;
int p[];
int b1,b2;
char ch[];
int num[][];
int cal(int b, int j, int i){
if(ch[j]=='|') return b|num[j][i];
if(ch[j]=='&') return b&num[j][i];
else return b^num[j][i];
}
int main()
{
int n,m;
p[]=;
for(int i=;i<;i++) p[i]=p[i-]*;
while(cin>>n)
{
XOR=OR=;
memset(num, , sizeof(num));
for(int i=;i<n;i++){
scanf(" %c %d", &ch[i], &m);
int t=;
while(m){
if(m&) num[i][t]=;
t++;
m>>=;
}
}
for(int i=;i<;i++){
b1=,b2=;
for(int j=;j<n;j++){
b1=cal(b1, j, i);
b2=cal(b2, j, i);
}
if(b1==&&b2==){//n and 1 相当于 n or 1 xor 1
OR^=p[i];
XOR^=p[i];
}
if(b1==&&b2==) OR^=p[i];
if(b1==&&b2==) XOR^=p[i];
}
int sum=;
if(OR) sum++;
if(XOR) sum++;
cout<<sum<<endl; if(OR) cout<<'|'<<' '<<OR<<endl;
if(XOR) cout<<'^'<<' '<<XOR<<endl;
}
}

Codeforces 878A - Short Program(位运算)的更多相关文章

  1. Codeforces Round #443 (Div. 2) C: Short Program - 位运算

    传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...

  2. Codeforces 879C/878A - Short Program

    传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...

  3. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  4. codeforces - 15C Industrial Nim(位运算+尼姆博弈)

    C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  5. Codeforces 868C Qualification Rounds - 位运算

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...

  6. CodeForces - 1230D(思维+位运算)

    题意 https://vjudge.net/problem/CodeForces-1230D 要组建一个小组,要求小组中每个人都不比所有人强,当一个人懂得一个算法但是另一个不懂那么前者认为他比后者强. ...

  7. 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 ...

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

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

  9. 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 ...

随机推荐

  1. jmeter链接数据库操作

    jmeter链接数据库操作步骤 首先要先下载mysql-connector-java-5.1.39-bin.jar驱动包 链接:https://pan.baidu.com/s/14F4rp4uH1hX ...

  2. 使用Inception-v3进行图像分类

    https://blog.csdn.net/sinat_27382047/article/details/80534234 https://www.jianshu.com/p/cc830a6ed54b ...

  3. 应用安全 - 中间件 - Apache - Apache POI

    CVE-2014-3529 Date2014 类型 注入XML外部实体访问外部实体资源或者读取任意文件 影响范围 Apache POI 3.10-FINAL及以前版本 复现 CVE-2016-5000 ...

  4. 【Linux开发】【Qt开发】Qt界面键盘、触摸屏、鼠标的响应设置

    USB键盘 经过一番搜索,发现对Qt键盘的支持主要关系到两个方面: 1. 键盘类型确定: 4.7以前的Qt版本,如果是PS2圆孔键盘,Qt编译时需加上选项:-qt-kbd-vr41xx(未测试):如果 ...

  5. Linux-Maven部署

    一.Maven是什么 二.Maven部署 1.环境信息: (1)centos7.3 (2)jdk1.8 (3)maven3.5.3 2.安装jdk (1)下载地址[http://www.oracle. ...

  6. [转帖]socat使用笔记

    socat使用笔记 https://blog.csdn.net/yangbingzhou/article/details/49783235 进行简单学习 centos 下面安装 yum install ...

  7. C函数调用过程原理及函数栈帧分析(转)

    在x86的计算机系统中,内存空间中的栈主要用于保存函数的参数,返回值,返回地址,本地变量等.一切的函数调用都要将不同的数据.地址压入或者弹出栈.因此,为了更好地理解函数的调用,我们需要先来看看栈是怎么 ...

  8. spring boot @Transactional的一个小坑

    同一个类Service下,有两个函数 method_1和 method_2,且method_1内部调用了method_2,那么希望method_2内部意外时,数据库回滚,那么一定要在method_1上 ...

  9. roslyn\csc.exe

    vs2019调试运行时提示roslyn\csc.exe错误时在nuget包管理器控制台里输入:  Update-Package Microsoft.CodeDom.Providers.DotNetCo ...

  10. 主机(windows10)虚拟机(ubuntu18)arm板(linux3.4)相互ping通

    实际中在主机上安装虚拟机,并在主机上通过网线连接arm板进行调试. 用网线将主机和arm板直接物理连接,且主机和arm必须处于同一个网段.(我们知道主机中的网卡具有路由器的功能) 其中arm板IP地址 ...