http://codeforces.com/contest/879/problem/C
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.
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 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.
3
| 3
^ 2
| 1
2
| 3
^ 2
3
& 1
& 3
& 5
1
& 1
3
^ 1
^ 2
^ 3
0
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.母数用000000不够,还需要与1111111结合,将其经过处理后数的每一位发生变化 0->? 1->?,
所以对于每个处理的数位我们有:
(0->0,1->0)none
(0->1,1->1)or 1
(0->1,1->0) xor 1
(0->0,1->1) and 1
2.位运算处理数后保持原样的操作有这三个 & 1 | 0 ^ 0
3.复习下位运算的优先级 AND>XOR>OR
#include <bits/stdc++.h>
using namespace std;
#define maxn 100000
typedef long long ll;
#define inf 2147483647
#define ri register int int n, x;
char ch;
int f0 = , f1 = ;
int AND = , XOR = , OR = ; int main() {
ios::sync_with_stdio(false);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin >> n;
for (int i = ; i <= n; i++) {
cin >> ch >> x;
if (ch == '|') {
f0 |= x;
f1 |= x;
}
if (ch == '&') {
f0 &= x;
f1 &= x;
}
if (ch == '^') {
f0 ^= x;
f1 ^= x;
}
}
int base = ;
int w0, w1; for (int i = ; i <= ; i++) {
w0 = f0 & ;
w1 = f1 & ;
if (w0 == ) {
if (w1 == )
AND += base;
} else {
if (w1 == ) {
AND += base;
XOR += base;
} else {
AND += base;
OR += base;
}
}
base <<= ;
f0 >>= , f1 >>= ;
}
cout << << endl
<< "& " << AND << endl
<< "^ " << XOR << endl
<< "| " << OR << endl; return ;
}