Description
问区间[a,b]中有多少个数字的二进制表示只有一个0
Input
两个整数a和b(1<=a<=b<=10^18)
Output
问区间[a,b]中有多少个数的二进制表示中只有一个0
Sample Input
5 10
Sample Output
2
Solution
水题,把所有满足条件的数暴力打表即可
Code
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
#define maxn 2222
ll a,b,ans[maxn],res;
void get_ans()
{
res=0;
for(int i=1;i<64;i++)
for(int j=0;j<i;j++)
{
ll temp=0ll;
for(int k=0;k<=i;k++)
if(k!=j)temp+=(ll)1<<k;
ans[res++]=temp;
}
}
int main()
{
get_ans();
while(~scanf("%I64d%I64d",&a,&b))
{
int cnt=0;
for(int i=0;i<res;i++)
if(ans[i]>=a&&ans[i]<=b)cnt++;
printf("%d\n",cnt);
}
return 0;
}