codeforces611B. New Year and Old Property
来源:互联网 发布:阅读软件塞班版 编辑:程序博客网 时间:2024/06/05 16:00
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 and1010 = 10102. Two of them (1012 and 1102) have the described property.
题意:给出两个数求在这个区间中的数用二进制表示下只有1个0的数个个数
解题思路:计算出两个的二进制位数然后对位数在其之间的直接计算边缘的dfs即可
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<list>#include<vector>using namespace std;int Count(long long n){int cnt=0;while(n){n>>=1;cnt++;}return cnt;}long long a,b,ans;void dfs(long long num,bool sign,int cnt,int std){if(cnt==std){if(num>=a&&num<=b&&sign)ans++;return ;}if(!sign)dfs(num*2,true,cnt+1,std);dfs(num*2+1,sign,cnt+1,std);}int main(){scanf("%lld%lld",&a,&b);int cnta=Count(a),cntb=Count(b);if(cnta==cntb){ans=0;dfs(1,false,1,cnta);printf("%lld\n",ans);}else {ans=0;dfs(1,false,1,cnta);dfs(1,false,1,cntb);for(int i=cnta+1;i<=cntb-1;++i){ans+=i-1;}printf("%lld\n",ans);}return 0;}
- codeforces611B. New Year and Old Property
- B. New Year and Old Property
- New Year and Old Property CodeForces
- New Year and Old Property CodeForces
- 611B. New Year and Old Property【模拟位运算】
- Codeforences Goodbye 2015 B New Year and Old Property
- codeforces - Goodbye2015B - New Year and Old Property(模拟)
- cf#gb2015-B - New Year and Old Property-暴力
- CodeForces 611 B. New Year and Old Property(水~)
- codeforces-611B-New Year and Old Property
- CodeForces 611B New Year and Old Property(DFS)
- CodeForces 611B - New Year and Old Property(搜索)
- 【Goodbye2015】Codeforces 611B New Year and Old Property【思维】
- codeforces 611B New Year and Old Property
- CodeForces 611BNew Year and Old Property
- Good Bye 2015-New Year and Old Property(二进制暴力枚举)
- Codeforces Good Bye 2015 B. New Year and Old Property (DFS)
- CF Good Bye 2015 B- New Year and Old Property(CF611B)
- mfc连接ACCESS2010及以上版本,部分“MicrosoftC++异常”处理
- hdoj5305【dfs】
- BSG白山极客挑战赛——B君的圆锥(计算几何)
- 决策树ID3、C4.5、C5.0以及CART算法之间的比较-并用scikit-learn决策树拟合Iris数据集
- hdu4545魔法串
- codeforces611B. New Year and Old Property
- Zabbix 使用 JMX 方式监控 Tomcat
- Java NIO:NIO概述
- poj 1721 CARDS(置换)
- CSS盒子模型
- codeforces510C. Fox And Names【拓扑排序】
- codeforces510B. Fox And Two Dots【dfs】
- 网站分析与优化
- 使用notepad++删除^M字符