您的位置:首页 > 编程语言 > Go语言

cfgoodbye2015 B

2016-01-03 19:25 387 查看

题目连接: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;

}


最后感谢卿学姐的模板:/article/6861430.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: