题解:CF1632C Strange Test

· · 题解

思路

发现答案为以下两种情况的最小值:

  1. 先一直加 a 直到 a \mid b = b。如果 a \neq b,额外进行一次操作,即 a := a \mid b
  2. 类似于第一种操作。先一直加 b 直到 a \mid b = b。如果 a \neq b,额外进行一次操作,即 a := a \mid b

AC Code

#include <bits/stdc++.h>
#define int long long
#define i64 long long
#define u64 unsigned long long
#define i128 __int128
#define u128 unsigned __int128
#define db double
#define pq priority_queue
#define mod 998244353
#define mod2 1000000007
#define pf1 push_front
#define pb1 push_back
#define pf2 pop_front
#define pb2 pop_back
#define inf 1073741823
#define INF 4611686018427387903
#define all(x) x.begin(), x.end()
using namespace std;
int T;

int a, b, _a, _b, res, ans;

inline void solve () {
    cin >> a >> b, ans = 1 << 30;
    _a = a, _b = b, res = 0;
    while ((_a | _b) != _b)
        _a++, res++;
    if (_a != _b)
        res++;
    ans = min(ans, res);
    _a = a, _b = b, res = 0;
    while ((_a | _b) != _b)
        _b++, res++;
    if (_a != _b)
        res++;
    ans = min(ans, res);
    cout << ans << '\n';
}
signed main () {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}