Nailoong and Operations

· · 题解

原背景

出题人题解,个人认为 *1400。

虽然但是,为啥大家写的都这么复杂,std 真的写得很短也没判任何 corner,这题原本是 T1 啊。

注意到若 y 中 p 代表的位为 0,两种操作是等价的。而为 1 时,两种操作分别可以 +p 或者不变。

考虑到这个为 1 时 +p 有什么用,注意到若 y 中 q=\operatorname{lowbit}(x-p) 代表的位为 0 且 +p 能让这一位变为 1 时,+p 是优的,否则,绝对不优。

所以这样就行了。

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&-x

int x,y;
void solve(){
    cin>>x>>y;
    while(x){
        int p=lowbit(x);
        x-=p;
        if(!x||!(y&p)){
            y|=p;
            continue;
        }
        int pp=lowbit(x);
        if(!(y&pp)&&((y+p)&pp)) y+=p;
    }
    cout<<y<<endl;
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int T;cin>>T;while(T--) solve();
    return 0;
}