cfgoodbye2015 B

题目连接:http://codeforces.com/contest/611/problem/B

Description:

B. New Year and Old Property

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.

Sample Input

5 10

Sample Output

2

Hint

n the first sample Limak’s interval contains numbers 5 = 101, 6= 110, 7 = 111, 8= 1000, 9 = 100 and 10 = 1010. Two of them (101 and 110) have the described property.

题意

给定两个数,在这两个数之间找到二进制数只有一个0的所有数。

收获

1.dfs(优先深度搜索),深度优先,从一个点一直往深处挖,知道达到最大。
对了,卿学姐这个剪去了子三代之后的左支(因为这些都是没用的数)。
map:
这里写图片描述
2.该题就是借用了深度这个概念,找到每一位开始的全是1的数,然后*2+1,也可以这样理解。
map:这里写图片描述
3.感觉递归搜索还是不怎么会,大致归纳如下:
①结束条件
②题目要求,数据处理和判断
③走左支
④走右支

代码

c++:#include<bits/stdc++.h>
using namespace std;
long long i,j;
int flag;
int ans=0;
int dfs(long long x,int flag){
if (x>j)
{
return 0;
}
if (x<=j&&x>=i&&flag==1)
{
ans++;
}
if (flag==0)
{
dfs(x*2,1);
}
dfs(x*2+1,flag); //此处flag不能换为1,两个作用,一个继续生成新的右支,即继续1->3->7->... ,还有就是剪去已经没有必要的左支。
}
int main(int argc, char const *argv[])
{
cin>>i>>j;
dfs(1,0);
cout<<ans<<endl;
return 0;
}

最后感谢卿学姐的模板:http://www.cnblogs.com/qscqesze/p/4501550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值