題目:
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;
}