跳到正文

题解

题解:CF1632C Strange Test

ssfwwz约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年5月5日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 2 分钟
题目
CF1632C

版本

  1. v1当前

题目:CF1632C

题目大意

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

思路

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

代码

C++
#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;
}
原文