题意:求[l,r]之间与n或运算最大值的最小值
思路:或运算要最大值且尽量小,那么n位数是1的时候ans同位数的位置要是0,反过来也是,而当ans<l的时候,那么l位数是1的地方ans也要为1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
unsigned n,l,r,t,ans;
int main(){
while (scanf("%u%u%u",&n,&l,&r) != EOF){
int bit = 0;
t = r;
while (t){
bit++;
t /= 2;
}
bit -= 1;
ans = 0;
for (int i = bit; i >= 0; i--){
t = ans + (1 << i);
if ((t <= r && (n & 1<<i) == 0) || (ans < l && (l & 1<<i)))
ans = t;
}
printf("%u\n",ans);
}
return 0;
}