B 二进制

题目链接: https://ac.nowcoder.com/acm/contest/8564/B

image-20210221185340310

image-20210221185431834

单纯与或者单纯或单纯异或都支持交换律,但是他们放在一起就不支持交换律了,比如1先和0与再和1或结果是1,但是1先和1或再和0与结果就不一样,可以设两个数,一个所有位都是0,一个所有位都是1,把这两个分别进行上述操作,最后得出来的结果按位对比,如果0变成了1且1变成了0则这个位肯定是异或1,如果0变成0且1变成0则这一位是与0,如果0变成1且1变成1这一位肯定是或1,按照这个规律可以求出这个数,任何一个数与1或0异或0都不变,所以可以设置三个数,一个全设为1,另外两个全设为0,当这个位是与0时,就用全为1的变量减去(1<<i)这一位上的数,另外两个同理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
#define debug freopen("in.txt","r",stdin); freopen("out.txt","w",stdout)
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN=1e6+100;
const double pi=acos(-1);
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const int SUB=-0x3f3f3f3f;
const int eps=1e-4;

int a=0,b=(1<<20)-1;
int main(){
ios;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int op,v;
cin>>op>>v;
if(op==1){
a&=v;
b&=v;
}
else if(op==2){
a|=v;
b|=v;
}
else{
a^=v;
b^=v;
}
}
int yu=(1<<20)-1,huo=0,yihuo=0;
for(int i=0;i<20;i++){
if( (a>>i)&1 ){
if((b>>i)&1) huo+=(1<<i);
else yihuo+=(1<<i);
}
else if(!((b>>i)&1)) yu-=(1<<i);
}
cout<<3<<'\n';
cout<<1<<' '<<yu<<'\n';
cout<<2<<' '<<huo<<'\n';
cout<<3<<' '<<yihuo<<'\n';
return 0;
}