A. Short Program
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

(这是一道好题啊

题意:给一定的步骤,求一个满足同样功能的步骤(步骤数小于5,不需要从原步骤里选

解题思路:先取两个典型的例子0和1023,按照input的步骤进行下去,分别得到答案a和b,然后按位分析如何由0和1023到a和b

按位分析:

0->0  1->0   先&0再^0

0->0  1->1   先&1再^0

0->1  1->0   先&1再^1

0->1  1->1   先&0再^1

对每一位进行上述的四种情况分析就行啦。

ac代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 using namespace std;
5 int x=0,y=1023;
6 int fun(int u,int i) {
7 if(u&(1<<i)) return 1;
8 return 0;
9 }
10 int main() {
11 ios::sync_with_stdio(false);
12 cin.tie(0);cout.tie(0);
13 int n,a;
14 cin>>n;
15 string s;
16 for(int i=0;i<n;++i) {
17 cin>>s>>a;
18 if(s[0]=='|') {x|=a; y|=a;}
19 if(s[0]=='^') {x^=a; y^=a;}
20 if(s[0]=='&') {x&=a; y&=a;}
21
22 }
23 int i=0,v1=0,v2=0;
24 while(i<=10) {
25 if(!fun(x,i) && fun(y,i)) v1+=(1<<i);
26 if(fun(x,i) && fun(y,i)) v2+=(1<<i);
27 if(fun(x,i) && !fun(y,i)) {
28 v1+=(1<<i);
29 v2+=(1<<i);
30 }
31 ++i;
32 }
33 cout<<2<<endl<<"& "<<v1<<endl<<"^ "<<v2<<endl;
34 return 0;
35 }

codeforces 878A的更多相关文章

  1. Codeforces 878A - Short Program(位运算)

    原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...

  2. Codeforces 879C/878A - Short Program

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

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 2V升3V芯片,输入2V输出3V可达1A

    PW5328B是一个恒定频率, 6引脚 SOT23电流模式升压转换器,用于小型低功耗应用. PW5328B的开关频率为 1.2MHz,允许使用微小的.低成本的电容器和电感器.内部软启动导致小涌流和延长 ...

  2. Development desciptor

    概述与作用: 部署描述符是用于描述Web应用程序的元数据,并为Java EE Web应用程序服务器部署和运行Web应用程序提供指令.从传统上来说,所有元数据都来自于部署描述符文件/WEB-INF/we ...

  3. Nacos 服务配置中心

    1.因为项目是微服务分布式项目,每个微服务都需要用到配置中心,所以第一步我们先在common中添加相应的依赖 <dependency> <groupId>com.alibaba ...

  4. Py集合,字符串的格式化,函数,便利

    可变与不可变 不可变指的是:重新赋值时,内存中的id值会变得 其中有:字符串,数字,元组 name="sb" v=id(name) print(v) name ="ale ...

  5. MySQL调优性能监控之performance schema

    一.performance_schema的介绍 performance:性能 schema:图(表)示,以大纲或模型的形式表示计划或理论. MySQL的performance schema 用于监控M ...

  6. etcd 性能优化实践

    https://mp.weixin.qq.com/s/lD2b-DZyvRJ3qWqmlvHpxg 从零开始入门 K8s | etcd 性能优化实践 原创 陈星宇 阿里巴巴云原生 2019-12-16 ...

  7. Java反序列化: 基于CommonsCollections4的Gadget分析 Java 序列化与反序列化安全分析

    Java反序列化: 基于CommonsCollections4的Gadget分析 welkin 京东安全 5天前 https://mp.weixin.qq.com/s/OqIWUsJe9XV39SPN ...

  8. Canvas实现弧线时钟

    最近试着用canvas元素的2d绘图函数做了一个弧线形的时钟. 我也没啥好说的,直接上代码: <div id="myclock"></div> <sc ...

  9. LOJ10065 北极通讯站

    Waterloo University 2002 北极的某区域共有 n 座村庄,每座村庄的坐标用一对整数 (x,,y) 表示.为了加强联系,决定在村庄之间建立通讯网络.通讯工具可以是无线电收发机,也可 ...

  10. Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像)

    Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像) 一.geoserver+openlayers+mysql主要设计思想 1.1 Geo ...