正妹吃月饼

P2431 正妹吃月饼

我一开始是a<=2^n-1<=b,直接输出n=(int)log(b+1)/log(2);

50分,没有仔细分析2^n-1<a但是2^(n+1)-1>b的情况。

这里考虑位运算

在l小于r的基础上从小到大|1,最后有多少个一加起来就可以了。

#include<iostream>
#define LL long long
using namespace std;
int main()
{
    int ans=0;
    LL l,r;
    cin>>l>>r;
    while((l|(l+1))<=r)
    l|=l+1;
    while(l)
    ans+=l&1,l>>=1;
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/war1111/p/7586712.html