[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3668
[算法]
从高位向低位贪心即可
时间复杂度 : O(30N)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXLOG 30
const int MAXN = 1e5 + ; int n,m,ans;
int value[MAXN],t[MAXN];
char op[]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool ok(int x,int val)
{
int num;
num = (val == ) ? : << x;
for (int i = ; i <= n; i++)
{
if (value[i] == ) num &= t[i];
if (value[i] == ) num |= t[i];
if (value[i] == ) num ^= t[i];
}
if (num & ( << x)) return true;
else return false;
} int main()
{ scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
scanf("%s%d",op,&t[i]);
if (strcmp(op,"AND") == ) value[i] = ;
if (strcmp(op,"OR") == ) value[i] = ;
if (strcmp(op,"XOR") == ) value[i] = ;
}
for (int i = MAXLOG; i >= ; i--)
{
if (ok(i,))
{
ans += << i;
continue;
}
if (ok(i,) && m >= << i)
{
m -= << i;
ans += << i;
}
}
printf("%d\n",ans); return ; }