Codeforces 879C Short Program

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.

 
题目的意思是,先给出一个n,然后紧接着跟着n行的程序,每一行含有一个操作符和一个操作数,操作符只有三种与,或,异或。同时规定所有的操作数都在0-1023之间。
要你把给出的程序缩短,缩短到5行之内。
思路:要你把程序缩短,因为只有三种操作符,那么显然所有的程序的结果都可以通过这三种操作符得到,那么我们只需要规定最后的输出一定是是三个,然后我们要做的就是求解三种操作符之后的操作数。因为要使得程序的结果相同,而且所有的操作数都在0-1023之间,那么我们可以将所有的操作对这10位二进制的数的影响记录下来。
其中或‘ | ’ ,很显然,一位1或上不论或上任何数字都是本身 ,所以这一位上的值是可以确定的。
同理与‘&’ ,很显然,与会和或相反,也就是0于上任何一个数字都是本身,那么这一位也是可以确定的。
如果程序遇到了异或‘^’  ,因为假如这一位上是1,那么遇上异或可能是1和0,如果是1和1那么这一位上就会是0,如果是0那么这一位上就不会改变。如果是0,那么遇上1,就会变成1,遇上0,那么就还是0;所以我们可以总结出,如果程序遇上异或,那么该位上的数值就要变成相反的数,也就是0-1,1-0;那么就要把所有位取反。
经过所有的操作符和操作数的影响,最后可以得出一个10位的影响数。
那么接下来就是把各个操作数取出来了,因为如果一位上是1,那么它可以通过或确定
那么先算或的,那么我们就可以加起来就好了,也就是把这一位上是1的加起来就是,或的操作数。
然后是与的,因为与确定的是0的位,那么我们就用一个初始值全部为1的初值来得出,也就是减去记录下来的10位数值。
最后是异或,这个更简单,直接或一下得出答案就好。
原文地址:https://www.cnblogs.com/yewa/p/7748396.html