http://codeforces.com/contest/878/problem/Ac++
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.app
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.this
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.spa
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.code
Output an integer k (0 ≤ k ≤ 5) — the length of your program.orm
Next k lines must contain commands in the same format as in the input.three
3
| 3
^ 2
| 1ip
2
| 3
^ 2ci
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.
如今有一個程序,輸入一個[0,1024)的數,而後通過一些位運算以後,輸出一個數。
如今讓你簡化中間的位運算的過程,使得不超過5步,使得答案和原程序同樣。
考慮每一位,只會存在4種狀況:
對於輸入的數字的每一位而言,要麼是1,要麼是0。而這些每一位的數輸出以後要麼變成了0,要麼就變成了1.
(1)0->0,1->0
(2)0->1,1->0
(3)0->0,1->1
(4)0->1,1->1
對於四種狀況,咱們均可以經過^和|就能夠解決,分狀況討論輸出便可。
#include<bits/stdc++.h> using namespace std; int n,a,b,p; string s; int main(){ cin>>n; a = 0,b = 1023; for(int i=0;i<n;i++){ cin>>s>>p; if(s[0]=='|'){ a|=p; b|=p; }else if(s[0]=='^'){ a^=p; b^=p; }else{ a&=p; b&=p; } } int ans1=0,ans2=0; for(int i=0;i<10;i++){ int a1=a&(1<<i); int b1=b&(1<<i); if(a1&&b1){ ans1|=(1<<i); } if(a1&&!b1){ ans2|=(1<<i); } if(!a1&&!b1){ ans1|=(1<<i); ans2|=(1<<i); } } cout<<"2"<<endl; cout<<"| "<<ans1<<endl; cout<<"^ "<<ans2<<endl; }