P2431 正妹吃月饼
求l到r之间有多少个二进制1


1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 LL ans; 7 char ch; 8 inline void read(LL &now) 9 { 10 int f=1; ch=getchar(); now=0; 11 while(ch>'9'||ch<'0') {if(ch=='-') f*=-1; ch=getchar();} 12 while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 13 now*=f; 14 } 15 16 int main() 17 { 18 LL l,r; 19 read(l); read(r); 20 while((l|(l+1))<=r) l|=l+1; 21 while(l) 22 { 23 ans+=l&1; 24 l>>=1; 25 } 26 cout<<ans<<endl; 27 return 0; 28 }