题目链接:http://codeforces.com/problemset/problem/611/B

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的数是很少的,那怎么推呢?可以知道10符合,101,1011,10111…… 以及110,1101,11011……等等,都是只有一个0的。那么就要从中找规律了。就比如1,它自己不符合,但是1<<1=10 就符合了,那么接下来(10<<1)+1==101, (101<<1)+1==1011……等都可以推下去。但是这组数据的都知识放在第2个位置,那么0在第三第四第n个位置的数又怎么推导呢?可知当(1<<1)+1==11,然后就可以推出110,1101了。所以就可以得出一个结论:当k的二进制数全为1,那么可以通过k生成一个符合和不符合的数。符合的数就直接*2+1继续推出一个符合的数就行了,他们的0全在同一个位置,且称他们在同一组吧;
而不符合的又可以生成一个符合的数和一个不符合的数,同时创造了两个新组,符合的数只能生成同一组的数,而不符合的数可以一直生成不同的组。
一个递归的过程。

代码如下:

 #include<stdio.h>
#include<string.h> long long a,b,sum = ; void dfs(long long i, int flag)
{
if(i>b)
return ; if(!flag)
{
dfs(i*,);
dfs(i*+,);
}
else
{
if(i>=a && i<=b)
sum++;
dfs(i*+, );
}
} int main()
{
scanf("%lld%lld",&a,&b);
dfs(,);
printf("%lld\n",sum);
return ;
}

Good Bye 2015 B. New Year and Old Property —— dfs 数学的更多相关文章

  1. Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  2. Good Bye 2015 B. New Year and Old Property 计数问题

    B. New Year and Old Property   The year 2015 is almost over. Limak is a little polar bear. He has re ...

  3. codeforces Good Bye 2015 B. New Year and Old Property

    题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...

  4. good bye 2015 B - New Year and Old Property

    题意:统计在n,m之间的数的二进制表示形式只有一个零的数目. 位运算模拟+dfs #include<iostream> #include<string> #include< ...

  5. Good Bye 2015 D. New Year and Ancient Prophecy

    D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...

  6. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...

  7. Codeforces Good Bye 2015 A. New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  8. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  9. Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

随机推荐

  1. 洛谷—— P1375 小猫

    https://www.luogu.org/problemnew/show/1375 题目描述 有2n只小猫站成一圈,主人小明想把它们两两之间用绳子绑住尾巴连在一起.同时小明是个完美主义者,不容许看到 ...

  2. mysql中TIMESTAMPDIFF简单记录

    1.  Syntax TIMESTAMPDIFF(unit,begin,end); 根据单位返回时间差,对于传入的begin和end不需要相同的数据结构,可以存在一个为Date一个DateTime 2 ...

  3. CSS3模拟IOS滑动开关

    前言 H5站点需要IOS滑动按钮的效果,想了想似乎CSS3能搞起,就折腾出来了...挺简单的..请看注释 效果 代码 <!DOCTYPE html> <html lang=" ...

  4. Engine中如何实现鼠标滚轮缩放反置?

    来自:http://zhihu.esrichina.com.cn/?/question/6666 [解决办法]:1,禁用IMapControl的默认鼠标滚轮事件.即设置IMapControl4.Aut ...

  5. iOS -- app全局字体设置

    方法一: 写一个UILabel(FontExtension)扩展重写initWithFrame(手写代码必走方法)和awakeFromNib(xib必走方法)当然UIButton.UITextView ...

  6. Android应用开发之所有动画使用详解

    题外话:有段时间没有更新博客了,这篇文章也是之前写了一半一直放在草稿箱,今天抽空把剩余的补上的.消失的这段时间真的好忙,节奏一下子有些适应不过来,早晨七点四十就得醒来,晚上九点四十才准备下班,好像最近 ...

  7. Dubbo zookeeper 初探

    先把zookeeper在本地给安装好, 安装方法参考:http://blog.csdn.net/wxwzy738/article/details/16330253 这里的话讲述了两个工程一个工程是提供 ...

  8. Java中字符串转为16进制表示

    Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...

  9. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  10. react 路由传参

    今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...