The year 2015 is almost over.
Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.
Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?
Assume that all positive integers are always written without leading zeros.
The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.
Print one integer – the number of years Limak will count in his chosen interval.
5 10
2
2015 2015
1
100 105
0
72057594000000000 72057595000000000
26
In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.
题目大意:
然你求从a到b中有多少个数的二进制数正好有一个0.
思路:
我们暴力预处理这样的数都是谁就行了。
枚举一个长度,再枚举这个0的位子,再一层for求和即可。
然后每次输入a,b,我们扫一遍这种数都哪些在【a,b】中统计起来就行了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll __int64
ll c[154545];
int main()
{
ll a,b;
while(~scanf("%I64d%I64d",&a,&b))
{
int cnt=0;
for(int len=0;len<=63;len++)
{
for(int j=0;j<=len;j++)
{
int o=0;
ll sum=0;
for(int k=0;k<=len;k++)
{
if(k!=j)
{
sum+=(ll)pow(2,k);
}
else o=k;
}
if(o!=len)
c[cnt++]=sum;
}
}
int output=0;
for(int i=0;i<cnt;i++)
{
if(c[i]>=a&&c[i]<=b)
{
output++;
}
}
printf("%d\n",output);
}
}