璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:cf_1918_c
problems
名称XOR-distance
题目编号1918C
题目链接codeforces.com/…
来源CodeForces
算法分类位运算, 贪心
难易程度一般般

XOR-distance

想法

距离就是越来越相近,也就是大的变小,小的变大。找到两数的最高的不相同位置,将后续的所有不相同位置尽可能变换(不包含最高不相同位置)。这样大的在变小,小的在变大,距离在接近。

代码实现

#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		long long int a, b, r, x = 0, temp;
		scanf("%lld %lld %lld", &a, &b, &r);
		if(a < b)swap(a, b);
		temp = 1LL << 62;
		while((temp & a) == (temp & b) && temp > 0)temp >>= 1;
		temp >>= 1;
		while(temp > 0) {
			if((temp & a) != 0 && (temp & b) == 0 && x + temp <= r)
				x += temp;
			temp >>= 1;
		}
		printf("%lld\n", abs((a^x) - (b^x)));
	}
	return 0;
}
/app/www/public/data/pages/icpc/problems/cf_1918_c.txt · 最后更改: 2024/02/19 13:27 由 温婕莺