1. 程式人生 > 實用技巧 >AcWing998 起床困難綜合徵(位運算)

AcWing998 起床困難綜合徵(位運算)

顯然高位越高越好,因此從高位往地位計算,判斷當前位填0或者填1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+10;
const int mod=1e9+7;
struct node{
    string op;
    int t;
}s[N];
int n;
int cal(int u,int x){
    int i;
    for(i=1;i<=n;i++){
        int now=(s[i].t>>u)&1;
        
if(s[i].op=="AND") x&=now; else if(s[i].op=="OR") x|=now; else x^=now; } return x; } int main(){ ios::sync_with_stdio(false); int m; cin>>n>>m; int i; for(i=1;i<=n;i++){ cin>>s[i].op>>s[i].t; } ll ans
=0; ll val=0; for(i=29;i>=0;i--){ int tmp1=cal(i,1); int tmp2=cal(i,0); if(val+(1<<i)<=m&&tmp2<tmp1){ val+=1<<i,ans+=tmp1<<i; } else{ ans+=tmp2<<i; } } cout<<ans<<endl; }
View Code