给你n个位运算,假设x属于[0,1023],让你求出特定的0-5个位运算,使这两种位运算后 答案相同。
POINT:
观察x属于[0,1023]这个条件,我们用10位全是1的1023和10位全是0的0,分别对给我们的n个位运算运算后。
得到2个10长度1 0串的。然后根据两个串的1,0变化成1或0来确定&^|运算。
所以答案必定是3个。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
const LL inf = 0x3f3f3f3f;
const int maxn = 5e5+23;
char s[maxn];
int x[maxn];
int n;
int a1[13];
int a2[13];
void doit1(int now){
for(int i=1;i<=n;i++){
if(s[i]=='|') now=now|x[i];
if(s[i]=='^') now=now^x[i];
if(s[i]=='&') now=now&x[i];
}
int k=1;
while(k!=11){
a1[k++]=now&1;
now>>=1;
}
}
void doit2(int now){
for(int i=1;i<=n;i++){
if(s[i]=='|') now=now|x[i];
if(s[i]=='^') now=now^x[i];
if(s[i]=='&') now=now&x[i];
}
int k=1;
while(k!=11){
a2[k++]=now&1;
now>>=1;
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>s[i]>>x[i];
}
doit1(1023);
doit2(0);
int b1=0,b2=0,b3=0;//& ^ |
// for(int i=10;i>=1;i--){
// printf("%d ",a1[i]);
// }
// printf("\n");
// for(int i=10;i>=1;i--){
// printf("%d ",a2[i]);
// }
// printf("\n");
b1=1023;
for(int i=10;i>=1;i--){
if(a1[i]==1&&a2[i]==1){
b3=b3+(1<<(i-1));
}
if(a1[i]==0&&a2[i]==0){
b1=b1-(1<<(i-1));
}
if(a1[i]==0&&a2[i]==1){
b2=b2+(1<<(i-1));
}
}
printf("3\n");
printf("^ %d\n| %d\n& %d\n",b2,b3,b1);
}