C. Short Program
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意:给几行表达式简化成五行以内的式子
做法:按位处理
每一位原来和现在有四种情况
1–>1 0–>0 not change
1–>0 0–>1 xor 1
1–>1 0–>1 or 1
1–>0 0–>0 or 1 ; xor 1
然后就看最终每一位是怎么变化的,对应上面哪种情况
我们可以设置一个全0(即a=0)的和一个全1(即b=1024-1)的数看最终变化,可以知道最多就两种操作 ^和 |所以代码就可以简化为两行式子,先|再^,根据变化求出两个操作数的那一位就行了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#define maxn 10010
#define maxe 100010
typedef long long ll;
using namespace std;
const double eps=1e-5;
const int inf=0x3f3f3f3f3f;
int main()
{
int n;
char op[4];
int x;
while(scanf("%d",&n)==1)
{
int a=0,b=1023;
for(int i=0;i<n;i++)
{
scanf("%s%d",op,&x);
if(op[0]=='|')
{
a|=x;
b|=x;
}
else if(op[0]=='^')
{
a^=x;
b^=x;
}
else if(op[0]=='&')
{
a&=x;
b&=x;
}
}
int ans1=0;
int ans2=0;
int a1,b1;
for(int i=0;i<10;i++)
{
a1=a&(1<<i);
b1=b&(1<<i);
if(a1&&b1)
{
ans1|=(1<<i);
}
else if(a1&&!b1)
{
ans2|=(1<<i);
}
else if(!a1&&!b1)
{
ans1|=(1<<i);
ans2|=(1<<i);
}
}
puts("2");
printf("| %d\n^ %d\n",ans1,ans2);
}
return 0;
}