跳到正文

题解

题解:CF1632C Strange Test

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

文章信息

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

版本

  1. v1当前

思路

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

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

AC Code

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