原题链接: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. 【ABAP系列】SAP ABAP控制单元格是否可编辑

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP控制单元格是否可 ...

  2. Js AJAX Event

    ;(function () { if ( typeof window.CustomEvent === "function" ) return false; function Cus ...

  3. 【Linux-设备树】编译器DTC

    DTC编译器:设备树源码DTS文件编译为二进制文件DTB. DTC编译器的作用:就是对设备树的源码的文件进行语法检查,根据linux的内核要求检查各个节点以及属性,将设备树源码编译生成二进制文件,以保 ...

  4. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

  5. c++多线程并发学习笔记(0)

    多进程并发:将应用程序分为多个独立的进程,它们在同一时刻运行.如图所示,独立的进程可以通过进程间常规的通信渠道传递讯息(信号.套接字..文件.管道等等). 优点:1.操作系统在进程间提供附附加的保护操 ...

  6. [LeetCode] 95. 不同的二叉搜索树 II

    题目链接 : https://leetcode-cn.com/problems/unique-binary-search-trees-ii/ 题目描述: 给定一个整数 n,生成所有由 1 ... n ...

  7. viewset的使用的方法

    viewset的使用方法中是怎么区分的update和crate modelViewSet中设置了这个类之后,设置了类成员变量:queryset 和 seriazlier class,所以POST操作直 ...

  8. linux查找进程id和杀死进程以及查看内存??

    ps 命令用于查看当前正在运行的进程 ps ax : 显示当前系统进程的列表 ps aux : 显示当前系统进程详细列表以及进程用户 -e 显示所有进程,环境变量 此参数的效果和指定"A&q ...

  9. html5移动端Meta的设置

    强制让文档的宽度与设备的宽度保持1:1,并且文档最大的宽度比例是1.0,且不允许用户点击屏幕放大浏览 1 <meta name="viewport" content=&quo ...

  10. android——屏幕适配

    一,基本概念 1:dip: 其实也就是dp,与像素无关 2:px: 像素,在安卓布局中不用px,因为每个手机像素不同,px显示的布局大小也就不同 3:dpi: 通俗点就是每英寸多少个像素,简称像素密度 ...