兔子的区间密码
思路
结论显然,我们从的二进制最高位出发,碰到的第一位不同的数即可得到从这一位向下,所有的二进制数位都可以得到为
,因此我们只需要从高位开始枚举即可,然后得到第
位是第一个不同位然后
即可得到答案。
代码
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T = read();
while(T--) {
ll l = read(), r = read();
int i;
for(i = 63; i >= 0; i--) if((l >> i) != (r >> i)) break;
cout << (1ll << i + 1) - 1 << endl;
}
return 0;
}

京公网安备 11010502036488号