很少做这种二进制的题 感觉很陌生 以后要多练练 感觉位运算好绕
把a二进制拆分 然后从最低位开始找 如果当前位是0的话 就判断一下把它变成1之后是不是比b小 小的话就变 有点贪心思想
代码如下
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
#define in = read();
typedef long long ll;
typedef unsigned int ui;
ll a , b;
int ans;
inline ll read(){
ll num = 0 , f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num*10 + ch - '0';
ch = getchar();
}
return num*f;
}
int main(){
a in; b in;
while((a|(a + 1)) <= b)
a |= a + 1;
while(a){
ans += a&1;
a >>= 1;
}
printf("%d" , ans);
}
//COYG
下面是Qiu.YF的代码 思路也很好理解 可以拿笔模拟一下
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
inline long long read(){
long long num=0,f=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
num=num*10+ch-'0';
ch=getchar();
}
return num*f;
}
inline long long getlen(long long x){
long long ans=0;
while(x){
if(x&1) ans++;
x>>=1;
}
return ans;
}
long long ans;
int main(){
long long a=read(),b=read();
long long len=(long long)log2(b)+1;
for(long long i=len-1;i>=0;--i){
long long x=1<<i;
if(!(x&b)) continue;
long long s=x^b;
for(long long j=0;j<i;++j) s|=(1<<j);
if(s<a) continue;
ans=std::max(ans,getlen(s));
}
ans=std::max(ans,getlen(b));
printf("%d",ans);
return 0;
}