按位贪心。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 100050 using namespace std; int n,m,op[maxn],a[maxn],ans=0,ret=0; char s[maxn]; int check(int x,int bit) { for (int i=1;i<=n;i++) { int nowbit=(a[i]>>bit)&1; if (op[i]==1) x&=nowbit; else if (op[i]==2) x|=nowbit; else x^=nowbit; } return x; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%s",s);scanf("%d",&a[i]); if (s[0]=='A') op[i]=1; else if (s[0]=='O') op[i]=2; else op[i]=3; } for (int i=30;i>=0;i--) { int l=-1,r=-1; l=check(0,i);r=check(1,i); if (l==1) ans+=(1<<i); else if ((r==1) && (ret+(1<<i)<=m)) {ans+=(1<<i);ret+=(1<<i);} } printf("%d ",ans); return 0; }