一头奶牛在研究数字的表示法,它只会二进制数,在泥地上它用一个脚印表示0,而用它的脚来表示1. 显然,它最多能表示4个位置上的1.
现给定一个范围[s,t] ( 1 <= s,t <= 20,000,000),请问这头牛可以表示其中的多少个数.
输入格式
只一行: 两个整数s 和t.
输出格式
只一行: 用少于4个1的二进制数可表示在[s,t]中的数的个数.
输入/输出例子1
输入:
100 105
输出:
5
样例解释
5 样例解释: 数 2进制数 1的个数 是否可表示 100 1100100 3 Yes 101 1100101 4 Yes 102 1100110 4 Yes 103 1100111 5 No 104 1101000 3 Yes 105 1101001 4 Yes
#include<bits/stdc++.h>
using namespace std;
int num_1(int x){
int cnt=0;
while(x){
++cnt;
x&=x-1;
}
return cnt;
}
int Start,End,L;
int main(){
cin>>Start>>End;
for(int i=Start;i<=End;++i){
if(num_1(i)<=4)++L;
}
cout<<L;
return 0;
}