cf878a(位运算)

来源:互联网 发布:java安卓4.4.4模拟器 编辑:程序博客网 时间:2024/06/06 03:08

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
题意:缩短位运算次数,能达到同样结果。

思路:分别用全1和全0的数进行一遍输入的操作,比较结果中的各个位,如果都是0,则都是0,如果都是1,则都是1,如果原为0后为1原为1后为0则异或,和原来不变则没有操作。

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){int n;while(~scanf("%d",&n)){int a=0,b=1023;char p[3];int x;for(int i=0;i<n;i++){scanf("%s %d",p,&x);if(p[0]=='&'){a=a&x;b=b&x;}else if(p[0]=='|'){a=a|x;b=b|x;}else{a=a^x;b=b^x;}}int y1=a&b;int y2=a|b;int ans=0;for(int i=0;i<10;i++){if((a>>i&1)&&!(b>>i&1))ans|=1<<i;}printf("3\n");printf("| %d\n",y1);printf("& %d\n",y2);printf("^ %d\n",ans);}}