牛客竞赛 1033 兔子的区间密码
题意
给定区间\(l-r\) , 从中选择两个整数\(x , y\) , 使得\(x\oplus y\) 最大 , 输出这个最大值 , 多组查询
思路
还是比较一般的思路 , 从高位到低位枚举
如果是一样就不需要考虑 ;
如果不一样时 , **首先肯定是r为 1 , l为 0 **
不妨设两个整数为\(a , b , a\le b\)
此时如果分头走 , 那么后面一直贴着墙壁走 , 维持异或为1
咦 , 那么不久解出来了嘛
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long int
inline int read() {
int ans = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
ans = ans * 10 + ch - '0';
ch = getchar();
}
return ans * f;
}
void solve() {
int l=read(),r=read();
if (l==r){cout<<0<<"\n";return ;}
for (int i = 63; i>=0; i--) {
if ((l>>i & 1) !=( r>>i&1)) {
cout<<(1ll<<(i+1)-1)<<"\n";
return ;
}
}
}
signed main() {
int t =read();
while (t--) solve();
return 0;
}

浙公网安备 33010602011771号