CodeForces 611B New Year and Old Property(DFS)
2016-05-15 15:02
489 查看
题意:给你l,r区间,问你[l,r]中,二进制中只含有1个0的数有多少个
思路:直接dfs跑,dfs(int x,int flag)表示现在是x,这个数里面是否只含有1个0。
Description
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
Input
Output
Input
Output
Input
Output
Input
Output
Hint
In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and1010 = 10102.
Two of them (1012 and 1102) have the described property.
思路:直接dfs跑,dfs(int x,int flag)表示现在是x,这个数里面是否只含有1个0。
#include<bits/stdc++.h> using namespace std; #define LL long long LL l,r,ans; void dfs(LL num,int flag) { if (num>r) return; if (num>=l && num<=r && flag==1) ans++; if (flag==0) dfs(num*2,1); dfs(num*2+1,flag); } int main() { scanf("%lld%lld",&l,&r); dfs(1,0); printf("%lld\n",ans); }
Description
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
Input
5 10
Output
2
Input
2015 2015
Output
1
Input
100 105
Output
0
Input
72057594000000000 72057595000000000
Output
26
Hint
In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and1010 = 10102.
Two of them (1012 and 1102) have the described property.
相关文章推荐
- SETSOCKOPT IOCTLSOCKET 设置非阻塞SOCKET函数
- Hadoop map与reduce数量
- Hadoop map与reduce数量
- Centos6.5 安装JDK
- Dubbo学习之目录
- Linux下卸载OpenJDK并安装Sun JDK的详细步骤
- apache基于端口的虚拟主机配置
- 初识linux 上
- linux下交叉编译jrtplib-3.9.1
- Hive学习
- linux 16进制编辑器
- 程序猿开发必会的Linux命令
- IT_linux_command--dstat
- Linux发行版 CentOS6.5 禁用防火墙步骤
- 虚拟机里面安装Openfiler 2.99
- Centos下kafka 单机配置部署详解
- tomcat配置虚拟目录和虚拟主机
- Poj 2186 Popular Cows
- Linux:前期总结
- [架构设计]第一讲:什么是架构