题目:
http://codevs.cn/problem/3311/
各种玄学WA,最后把数组调大,然后从第31位开始拆分就过了
为啥开long long从第32位拆分会WA?如果有dalao知道,请留言,谢谢;
贪心:
从高位到低位,如果选0和1都一样,那么选0,将”机会”留给较小位的1;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll MAXN=3000001;
string s;
ll a[MAXN],num[MAXN];
ll n,m,ans;
ll calc(ll x)
{
for(ll i=1;i<=n;i++)
{
if(a[i]==1) x=(x&num[i]);
else if(a[i]==2) x=(x|num[i]);
else x=(x^num[i]);
}
return x;
}
void solve()
{
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++)
{
cin>>s,scanf("%lld",&num[i]);
if(s=="AND") a[i]=1;
else if(s=="OR") a[i]=2;
else a[i]=3;
}
ll t=calc(0);
for(ll i=31;i>=0;i--)
{
if(ans+(1<<i)>m)//如果超了直接跳过;
continue;
else if(calc(1<<i)>t)
ans+=(1<<i);
}
cout<<calc(ans)<<'\n';
return;
}
int main()
{
solve();
return 0;
}