C. 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行的程序使得运算结果和给出程序的运算结果相同 思路:经过如果操作之后,总共十位二进制数,有:确定为1的,确定为0的,不变的,是原来的相反的,然后分别讨论四种
情况
*/
#include <bits/stdc++.h> #define MAXN 500005
#define MAXK 2 using namespace std; int n;
char str[MAXN][MAXK];
int a[MAXN];
int ora,xora,anda; inline int cal(int x){
for(int i=;i<n;i++){
switch(str[i][]){
case '^':
x^=a[i];
break;
case '&':
x&=a[i];
break;
case '|':
x|=a[i];
break;
} }
return x;
} inline void init(){
ora=;
xora=;
anda=;
} int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s%d",str[i],&a[i]);
}
int pos=cal();
for(int i=;i<;i++){
int cur=cal((<<i));
if(( cur&(<<i) )==&&( pos&(<<i) )==){//如果这一位肯定为0 }else if(( cur&(<<i) )!=&&( pos&(<<i) )!=){//这位肯定为1
anda+=(<<i);
ora+=(<<i);
}else if(( cur&(<<i) )==&&( pos&(<<i) )!=){//这位和原来相反
anda+=(<<i);
xora+=(<<i);
}else{//这位不变
anda+=(<<i);
}
}
printf("3\n");
printf("& %d\n",anda);
printf("| %d\n",ora);
printf("^ %d\n",xora);
return ;
}

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 - 位运算

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

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

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

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

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

  5. Codeforces Round #443 (Div. 2)

    C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...

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

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

  7. 【Codeforces Round #443 (Div. 2) C】Short Program

    [链接] 我是链接,点我呀:) [题意] 给你一个n行的只和位运算有关的程序. 让你写一个不超过5行的等价程序. 使得对于每个输入,它们的输出都是一样的. [题解] 先假设x=1023,y=0; 即每 ...

  8. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  9. Codeforces Round #443 Div. 1

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

随机推荐

  1. RMQ问题第一弹

    今天,我给大家分享一下我在学习 RMQ 问题过程中对该问题的理解. RMQ (Range Minimum/Maximum Query ):中文名为"区间最值查询".RMQ 问题指的 ...

  2. Bootstrap笔记合集

    一. 为了简化操作,方便使用,Bootstrap通过定义四个类名来控制文本的对齐风格: ☑   .text-left:左对齐 ☑   .text-center:居中对齐 ☑   .text-right ...

  3. JS中关于数组的内容

      前  言 LIUWE 在网站制作过程中,数组可以说是起着举足轻重的地位.今天就给大家介绍一下数组的一些相关内容.例如:如何声明一个数组和在网站制作过程中我们常用的一些数组的方法.介绍的不好还请多多 ...

  4. JVM(五)内存(Heap)分配

    前面的两小节,我分享了一下JVM的垃圾回收算法和垃圾回收器,本节中,我们来看看JVM的内存分配到底是如何进行的,作为对前面两节内存回收的补充. 从前面的内存回收中我们了解到,Hotspot JVM中的 ...

  5. 五年 Web 开发者 star 的 github 整理说明

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:樊东东 前端从业几年,积累了不少github开源库. 有时候想查阅以前star的库,但不好找,github大多库都是英文说明,对中文 ...

  6. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ----初始化一个线程

    使用线程的一个常见问题就是如何能够在一个线程开始运行之前,适当地将它初始化.初始化最常见的理由就是为了调整优先权.另一个理由是为了在SMP 系统中设定线程比较喜欢的 CPU.第10 章谈到 MFC 时 ...

  7. 【OOM】GC overhead limit exceeded

    我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...

  8. MyBatis注解select in参数

    /** *  * @param ids '1,2,3' * @return */ @Select("select * from user_info where id in (${ids})& ...

  9. Android打包版本号设置

    之前没有设置过打包的命名,每次打包都是默认的"app-realease.apk",之后手动修改名字来显示出它是一个新版本. 晚上学习了如何配置打包名称,很简单,修改build.gr ...

  10. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...