Good Bye 2015B(模拟或者二进制枚举)

B. New Year and Old Property
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

Print one integer – the number of years Limak will count in his chosen interval.

Examples
Input
5 10
Output
2
Input
2015 2015
Output
1
Input
100 105
Output
0
Input
72057594000000000 72057595000000000
Output
26
Note

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的数的个数;

方法1:(模拟)这是我最先想到的方法,然而代码比较复杂,还错了好多发,果然我还是一个小渣渣;

代码:

 #include <bits/stdc++.h>
#define ll long long
#define MAXN 100
using namespace std; int get_erjinzhi(ll n, int a[]) //*****将n转化为二进制存储到a数组中
{
int k=, xx[MAXN];
while(n)
{
xx[k++]=n%;
n/=;
}
for(int i=k-, j=; i>=; i--, j++)
a[j]=xx[i];
return k;
} int cmp(int a[], int yy[], int cnt) //*****比较a,yy的大小
{
for(int i=; i<cnt; i++)
{
if(a[i]>yy[i]) return ;
if(a[i]<yy[i]) return ;
}
return ;
} int main(void)
{
ll x, y, ans=;
int a[MAXN], b[MAXN], aa[MAXN], bb[MAXN];
cin >> x >> y;
if(x==&&y==)
{
cout << "" << endl;
return ;
}
if(x==) x++;
int cnt1=get_erjinzhi(x, a);
int cnt2=get_erjinzhi(y, b);
for(int i=; i<cnt1; i++)
{
aa[i]=;
}
aa[]=;
if(cmp(aa, a, cnt1))
{
if(cnt1==cnt2)
{
if(cmp(b, aa, cnt1))
ans++;
}
else ans++;
}
for(int i=; i+<cnt1; i++)
{
swap(aa[i], aa[i+]);
if(cmp(aa, a, cnt1))
{
if(cnt1==cnt2)
{
if(cmp(b, aa, cnt1)) ans++;
}
else ans++;
}
}
for(int i=; i<cnt2; i++)
{
bb[i]=;
}
bb[]=;
int k=;
if(cnt1!=cnt2)
{
while(cmp(b, bb, cnt2)&&k<cnt2)
{
ans++;
swap(bb[k], bb[k+]);
if(cmp(b, bb, cnt2)==)
{
ans++;
break;
}
k++;
}
}
for(int i=cnt1+; i<cnt2; i++)
{
ans+=i-;
}
cout << ans << endl;
return ;
}

方法2:(二进制枚举,看了别人代码才想到的,再一次证明了我是个渣渣)

思路:

二进制运算的姿势一定要摆对,不然得做好久…
举个例子:
10000-1=1111
1111-1=1110
1111-10=1101
1111-100=1011

代码:

 #include <bits/stdc++.h>
#define ll long long
using namespace std; int main(void)
{
ll a, b, ans=;
cin >> a >> b;
for(int i=; i<=; i++)
{
for(int j=; j<i-; j++)
{
ll temp=(1ll<<i)--(1ll<<j);
if(temp>=a && temp<=b) ans++;
}
}
cout << ans << endl;
return ;
}

方法3:(dfs)

 #include <bits/stdc++.h>
#define ll long long
using namespace std; ll ans=, a, b; void dfs(ll x, ll flag)
{
if(x>b) return;
if(x>=a&&x<=b&&flag) ans++;
if(flag==) dfs(x*, );
dfs(x*+, flag);
} int main(void)
{
cin >> a >> b;
dfs(, );
cout << ans << endl;
return ;
}
上一篇:基于变分自编码器(VAE)利用重建概率的异常检测


下一篇:《剑指offer》第二十八题(对称的二叉树)