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 由 温婕莺