题解 | x_to_y_2

x_to_y_2

https://www.nowcoder.com/practice/7c658c72c26643d681546085aa6dcd8b

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using PII=pair<ll,ll>;
using PIII=pair<int,pair<int,int>>;
const ld ESP = 1e-10;
const ld PI = acosl(-1);
const int N=1e5+10;
const int M=2e5+10;
// const int mod = 1000000007;
const int mod = 998244353;
//随机化
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 1000000000);
// cout<<fixed<<setprecision(10);

void solve(){
    ll x,y;
    cin>>x>>y;
    int cnt=0;
    while(x){
        int ok=1;
        for(int i=0;i<=61;i++){
            if((x>>i&1ll)&&((y>>i&1ll)==0)){
                ok=0;
                break;
            }
        }
        if(ok){
            if(x!=y)cout<<cnt+1<<'\n';
            else cout<<cnt<<'\n';
            return ;
        }
        x>>=1;
        cnt++;
    }
    if(x!=y)cout<<cnt+1<<'\n';
    else cout<<cnt<<'\n';    
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int _=1;
    cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

全部评论

相关推荐

12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务