题解:CF1632C Strange Test

· · 题解

题目:CF1632C

题目大意

让 a=b,每一步可以使 a 增加一、b 增加一或 a 变为 a|b,问最小的操作次数。

思路

按照题意模拟。先让 a 加到 a|b=b,或让 b 加到 a|b=b,再执行一次异或操作即可。 注意特判 a 大于 b 的情况(只能 b++)。

代码

#include<bits/stdc++.h>
using namespace std;
int t,a,b,x,y,cnt,ans;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if(a>=b)
        {
            cout<<a-b<<endl;
            continue;
        }
        ans=INT_MAX;
        cnt=0;
        x=a,y=b;
        while((x|y)!=y)
        {
            x++;
            cnt++;
        }
        if(x!=y) cnt++;
        ans=min(ans,cnt);
        cnt=0;
        x=a,y=b;
        while((x|y)!=y)
        {
            y++;
            cnt++;
        }
        if(x!=y) cnt++;
        ans=min(ans,cnt);
        cout<<ans<<endl;
    }
    return 0;
}