欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-15 14:39:47
...

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;
}