Codeforces Round #443 (Div. 1) CodeForces - 878 A

Short Program

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.


記錄一下全爲 1 的數和全爲 0 的數進行一次操作後得到的數a,b;

然後遍歷a和b的全部二進制位;

當a[i]=1,b[i]=1時,這個 i 的位置 | 上一個1;

當a[i]=1,b[i]=0時,不變;

當a[i]=0,b[i]=1時,這個 i 的位置 ^ 上一個 1;

當a[i]=0,b[i]=0時,這個 i 的位置 & 上一個 0;

也就是說最後一定會在三步內得到答案;

#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const ll mod=998244353;
int d[40][3];
int main(){
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	string s;
	int x;
	int a=(1<<10)-1,b=0;
	for(int i=1;i<=n;i++){
		cin>>s>>x;
		if(s[0]=='|'){
			a|=x;
			b|=x;
		}
		else if(s[0]=='^'){
			a^=x;
			b^=x;
		}
		else{
			a&=x;
			b&=x;
		}
	}
	for(int i=0;i<=9;i++) d[i][1]=(1&(a>>i));
	for(int i=0;i<=9;i++) d[i][2]=(1&(b>>i));
	int k1=0,k2=0,k3=0;
	int s1=0,s2=(1<<10)-1,s3=0;
	for(int i=0;i<10;i++){
		if(d[i][1]==0&&d[i][2]==1){//^1
			k1=1;
			s1+=(1<<i);
		}
	}
	for(int i=0;i<10;i++){
		if(d[i][1]==0&&d[i][2]==0){//&0
			k2=1;
			s2-=(1<<i);
		}
	}
	for(int i=0;i<10;i++){
		if(d[i][1]==1&&d[i][2]==1){//|1
			k3=1;
			s3+=(1<<i);
		}
	}
	int ans=k1+k2+k3;
	cout<<ans<<endl;
	if(k1){
		cout<<"^"<<" "<<s1<<endl; 
	}
	if(k2){
		cout<<"&"<<" "<<s2<<endl;
	}
	if(k3){
		cout<<"|"<<" "<<s3<<endl;
	}
	return 0;
}
發佈了207 篇原創文章 · 獲贊 45 · 訪問量 8298
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章