Good Bye 2015 B. New Year and Old Property —— dfs 数学
题目链接:http://codeforces.com/problemset/problem/611/B
2 seconds
256 megabytes
standard input
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.
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.
Print one integer – the number of years Limak will count in his chosen interval.
5 10
2
2015 2015
1
100 105
0
72057594000000000 72057595000000000
26
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 数学的更多相关文章
- 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 ...
- 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 ...
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
- good bye 2015 B - New Year and Old Property
题意:统计在n,m之间的数的二进制表示形式只有一个零的数目. 位运算模拟+dfs #include<iostream> #include<string> #include< ...
- 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 ...
- 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 ...
- 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 ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
- 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 ...
随机推荐
- 新华三孟丹:NFV资源池实现中的技术探讨
近日,在第三届未来网络发展大会SDN/NFV技术与应用创新分论坛上,新华三解决方案部架构师孟丹女士发表了主题为<NFV资源池实现中的技术探讨>的主题演讲. 孟丹指出,新华三的NFV核心理念 ...
- 1005 Spell It Right
1005 Spell It Right Given a non-negative integer N, your task is to compute the sum of all the dig ...
- Java-ArrayList使用技巧---从第一个List中去除所有第二个List中与之重复的元素
需求:从 mAllList 中去除所有 mSubList 中与之重复的元素 测试数据:mAllList 中包含100000个无序无重复字符串,mSubList 中包含50000个无序无重复字符串 方法 ...
- KVC技巧二则
说两个与KVC相关的技巧. 1.KVC与字典 有时候我们需要取出嵌套字典中的某个键的值.例如某个嵌套字典: NSDictionary *dict = @{@"subDict":@{ ...
- Gvim 和 Opencv编译
好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...
- 转:MapReduce原理与设计思想
转自:http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html 简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张 ...
- 关于Label::createWithBMFont中资源文件使用的坑爹问题解决方式
1.问题 使用Label的createWithBMFont,结果.fnt的资源总是找不到或者获取数据失败.原来.fnt资源的使用须要配合该资源的.png共同 使用,如bitmapFontTest3.f ...
- mysql主从一致问题
https://www.cnblogs.com/gomysql/p/3662264.html
- vue2 less less-loader 的用法
LESS基础语法 我们一起来学习一下LESS的基础语法,LESS的基础语法基本上分为以下几个方面:变量.混合(Mixins).嵌套规则.运算.函数.作用域等.这些基础语法需要我们先牢牢的掌握住,然后才 ...
- 电源滤波电容在PCB中正确的布线方法!
电源滤波电容在PCB中正确的布线方法! 错误的电源滤波电容布线方法. 1.很多人朋友在设计的时候喜欢加宽这个电源的走,这个是一个很好的方法,但是他们如果一不小心就会忽略电容的布线. 下面的电容布线看起 ...