C. Short Program

Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.

In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.

Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.

Input

The first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.

Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.

Output

Output an integer k (0 ≤ k ≤ 5) — the length of your program.

Next k lines must contain commands in the same format as in the input.

Examples
input
3
| 3
^ 2
| 1
output
2
| 3
^ 2
input
3
& 1
& 3
& 5
output
1
& 1
input
3
^ 1
^ 2
^ 3
output
0
Note

You can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.

Second sample:

Let x be an input of the Petya's program. It's output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.

题意:给一个任意数x,进行位运算,求怎么简化到不超过5次。

分析:看出每次的操作数不超过2^10-1,先用0000000000,1111111111,进行题意的操作,发现规律,01-----> 0/1,通过 | ^ & 运算使得它成立。

#include <bits/stdc++.h>

using namespace std;

bool calc(int a,int i) {
if(a&(<<i)) return ;
return ;
} int main()
{
int n;
int x = ,y = ; cin>>n;
while(n--) {
char str[];
int t;
scanf("%s%d",str,&t); if(str[]=='|') x|=t,y|=t; if(str[]=='&') x&=t,y&=t; if(str[]=='^') x^=t,y^=t; } int v1 = ; // |
int v2 = ; // ^
int v3 = ;
for(int i = ; i < ; i++) {
if(calc(x,i)&&calc(y,i)) v1 = v1 + (<<i);
if(calc(x,i)&&!calc(y,i)) v2 = v2 + (<<i);
//if(!calc(x,i)&&calc(y,i))
if(!calc(x,i)&&!calc(y,i)) v3 = v3 - (<<i);
} printf("3\n"); printf("| %d\n",v1);
printf("^ %d\n",v2);
printf("& %d\n",v3); return ;
}

Codeforces Round #443 (Div. 2)的更多相关文章

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

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

  2. Codeforces Round #443 Div. 1

    A:考虑每一位的改变情况,分为强制变为1.强制变为0.不变.反转四种,得到这个之后and一发or一发xor一发就行了. #include<iostream> #include<cst ...

  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 #443 (Div. 1) B. Teams Formation

    B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...

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

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

  7. Codeforces Round #443 (Div. 1) C. Tournament

    题解: 思路挺简单 但这个set的应用好厉害啊.. 我们把它看成图,如果a存在一门比b大,那么a就可以打败b,a——>b连边 然后求强联通分量之后最后顶层的强联通分量就是能赢的 但是因为是要动态 ...

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

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

  9. 【Codeforces Round #443 (Div. 2) A】Borya's Diagnosis

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

随机推荐

  1. Spark遇到的报错和坑

    1. Java版本不一致,导致启动报错. # 解决方法: 在启动脚本最前边添加系统参数,指定Java版本 export JAVA_HOME=/usr/java/jdk1..0_181-amd64/jr ...

  2. vue-vli3创建的项目配置热更新

    vue-vli3创建的项目配置热更新 问题描述:使用vue-cli3创建的项目,修改代码之后,浏览器页面不会自动刷新,然而之前使用webpack初始化的vue项目修改代码之后浏览器会重新加载一下,因为 ...

  3. 2019.03.28 读书笔记 关于lock

    多线程就离不开lock,lock的本质是一个语法糖,采用了监视器Monitor. lock的参数,错误方式有很多种,只需要记住一种:private static readonly object loc ...

  4. 用js语句控制css样式

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. Java学习第二十四天

    1:多线程(理解) (1)JDK5以后的针对线程的锁定操作和释放操作 Lock锁 (2)死锁问题的描述和代码体现 (3)生产者和消费者多线程体现(线程间通信问题) 以学生作为资源来实现的 资源类:St ...

  6. 【Ubuntu】Vritual Box 复制方式克隆

    重装系统后之前安装的虚拟机的镜像全都不见了 ,因为重装系统盘C盘会全部重新被格式化. VtritualBox如果没有指定虚拟机存放位置,默认是会放在C盘的,C:\Users\Administrator ...

  7. 求大神帮忙解决一下c#winfrom bindingNavigatorPositionItem加载直接跳转问题

    c# winfrom bindingNavigatorPositionItem控件问题加载直接跳转到相对应的行的问题

  8. 项目搭建系列之二:SpringMVC框架下配置MyBatis

    1.什么是MyBatis? MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis ...

  9. 记升级一次的http2学习

    首先,就先对比下http2和http1.X的区别和升级它的优势吧. 在 HTTP .X 中,为了性能考虑,我们会引入雪碧图.将小图内联.使用多个域名等等的方式.这一切都是因为浏览器限制了同一个域名下的 ...

  10. input placeholder 在chrome 浏览器自动填充时,背景色覆盖原有背景图片问题。

    user-block-name, .user-block-pwd { margin-bottom: 10%; text-align: center; position: relative; } .us ...