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 ...
随机推荐
- ES6之Array.includes()函数
一.定义 includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false. 二.语法 arr.includes(searchElement) arr.includ ...
- 洛谷—— P2183 巧克力
https://www.luogu.org/problemnew/show/P2183 题目描述 佳佳邀请了M个同学到家里玩.为了招待客人,她需要将巧克力分给她的好朋友们.她有N(1<=N< ...
- CDOJ 3 BiliBili, ACFun… And More! 模拟
原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- SVG动画实践篇-音量变化效果
git 地址:https://github.com/rainnaZR/svg-animations/tree/master/src/pages/step2/volumn 说明 这个动画的效果就是多个线 ...
- Go -- pprof协程监控
go中有pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一 ...
- Android获取窗口可视区域大小: getWindowVisibleDisplayFrame()
getWindowVisibleDisplayFrame()方法 getWindowVisibleDisplayFrame()是View类下的一个方法,从方法的名字就可以看出,它是用来获取当前窗口可视 ...
- mysql 数据库查询优化
从上图可以看出,计算机系统硬件性能从高到代依次为: CPU——Cache(L1-L2-L3)——内存——SSD硬盘——网络——硬盘 由于SSD硬盘还处于快速发展阶段,所以本文的内容不涉及SSD相关应用 ...
- python解析xml文件时使用ElementTree和cElementTree的不同点;iter
在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...
- cocos2d-x 事件分发机制 ——触摸事件监听
cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...