从高位到低位按位枚举,贪心。如果该位填1比填0结果优且填1不会超出m限制,那就填1,否则填0
1 /*by SilverN*/ 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 using namespace std; 7 const int mxn=100020; 8 int n,m; 9 int p[35]; 10 int op[mxn],a[mxn]; 11 int ans=0; 12 int pro(int x){ 13 for(int i=1;i<=n;i++){ 14 if(op[i]==1)x=x&a[i]; 15 else if(op[i]==2)x=x|a[i]; 16 else x=x^a[i]; 17 } 18 return x; 19 } 20 int main(){ 21 scanf("%d%d",&n,&m); 22 int i,j; 23 char c[5]; 24 for(i=1;i<=n;i++){ 25 scanf("%s%d",c,&a[i]); 26 if(c[0]==‘A‘)op[i]=1; 27 else if(c[0]==‘O‘)op[i]=2; 28 else op[i]=3; 29 } 30 for(i=0;i<=30;i++) p[i]=1<<i; 31 for(i=30;i>=0;i--){ 32 if(ans+(1<<i) > m)continue;//只能填0 33 if( pro(1<<i) > pro(0) )ans+=(1<<i); 34 } 35 printf("%d\n",pro(ans)); 36 return 0; 37 }
时间: 2024-10-22 20:15:12