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.
DFS,我是真的菜啊。
具体规律:
1:因为一个数的二进制是唯一的,所以不会产生重复。
2:对于任意一个数x,x*2+1的二进制中零的个数与x相同。
3:如果一个数x的二进制不含0,那么2*x的二进制会比x多一个0.
上代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstring>
using namespace std;
long long l,r;
long long ans;
void dfs(long long x,int flag)
{
if(x>r)return;
if(x<=r && x>=l && flag==1)
ans++;
if(flag==0)dfs(2*x,1);
dfs(2*x+1,flag);
return;
}
int main()
{
while(scanf("%lld %lld", &l,&r) != EOF)
{
ans = 0;
dfs(1,0);
cout << ans << endl;
}
return 0;
}
水波。