611B-New Year and Old Property
time limit per test2 seconds memory limit per test256 megabytes
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.
input
5 10
output
2
input
2015 2015
output
1
input
100 105
output
0
input
72057594000000000 72057595000000000
output
26
题目链接:cf-611B
题目大意:求[a,b]范围内,二进制有且只有一个0的数字有多少个
题目思路:直接暴力出所有只含有一个0的数字,判断这个数字是否在[a,b]范围内
以下是代码:
#include<iostream>
#include<string>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
ll ret[70];
ll a,b;
ll ans = 0;
#define LL long long
LL qpow(LL a, LL n) {
LL ret = 1;
while (n)
{
if (n & 1) ret = ret * a;
a = a * a;
n>>= 1;
}
return ret;
}
void solve()
{
ret[0] = 1;
for (ll i = 1; i < 64; i++)
{
ret[i] = ret[i - 1] + qpow(2,i);
}
}
void getans()
{
for (ll i = 0; i < 64; i++)
{
ll tmp = qpow(2,i);
for (ll j = i + 1; j < 64; j++)
{
if(ret[j] - tmp >= a && ret[j] - tmp <= b) ans++;
}
}
}
int main(){
solve();
cin >> a >> b;
getans();
cout << ans << endl;
return 0;
}