题解:CF2074C XOR and Triangle

Problem

Statement

给定 \(x\),问是否存在 \(y\),使得 \(x,y,x\oplus y\) 可以构成一个三角形。

Solution

\(cnt = \operatorname{popcount(x)}\)

首先打表发现,当 \(cnt = 1\)\(x = 2^{cnt}-1\) 时无解的。

因为 \(cnt = 1\) 时如果说 \(x\)\(1\) 的那一位 \(y\) 并不为 \(1\)\(x + y = x \oplus y\),反之就是 \(x + x \oplus y = y\),不满足。

\(x = 2^{cnt} - 1\) 也同理。

然后依旧是打表,经过多组小数据的测试我们发现 \(y = 2^{k} + 1\),也就是说我们去枚举 \(0 \sim \log_2{x} - 1\) 找到任意一个 \(y\) 就可以 \(O(\log x)\) 实现。

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
int Test, x;

inline bool Chk (int x, int y, int z) { return (x + y > z) && (x + z > y) && (y + z > x); }

inline void Solve() {
	cin >> x;
	int tot1 = __builtin_popcount(x);
	if (tot1 == 1) {
		cout << "-1" << endl;
		return;
	}
	if ((1 << tot1) - 1 == x) {
		cout << "-1" << endl;
		return;
	}
	int _x = x;
	for (int i = 0; i < log2(x); i ++) {
		if (!((x >> i) & 1)) {
			_x -= (1 << i);
			break;
		}
	}
	cout << _x << endl;
	return;
}

signed main() {
	cin >> Test;
	while (Test --) Solve();
	return 0;
}
posted @ 2025-03-12 10:28  xAlec  阅读(156)  评论(0)    收藏  举报