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;
}
推荐阅读
-
Codeforces Round #655 (Div. 2) A. Omkar and Completion
-
Codeforces Round #656 (Div. 3)D. a-Good String(递归+dfs)
-
Codeforces Round #487 (Div. 2)
-
CodeForces 1324 - Codeforces Round #627 (Div. 3)
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #649 (Div. 2) C-Ehab and Prefix MEXs
-
Educational Codeforces Round 71 (Rated for Div. 2)E. XOR Guessing
-
Codeforces Round #659 (Div. 2) A. Common Prefixes(字符串,思维)
-
Codeforces Round #610 (Div. 2)
-
Codeforces Round #670 (Div. 2)