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
  1. 5 10
Output
  1. 2
Input
  1. 2015 2015
Output
  1. 1
Input
  1. 100 105
Output
  1. 0
Input
  1. 72057594000000000 72057595000000000
Output
  1. 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的数的个数;

方法1:(模拟)这是我最先想到的方法,然而代码比较复杂,还错了好多发,果然我还是一个小渣渣;

代码:

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define MAXN 100
  4. using namespace std;
  5.  
  6. int get_erjinzhi(ll n, int a[]) //*****将n转化为二进制存储到a数组中
  7. {
  8. int k=, xx[MAXN];
  9. while(n)
  10. {
  11. xx[k++]=n%;
  12. n/=;
  13. }
  14. for(int i=k-, j=; i>=; i--, j++)
  15. a[j]=xx[i];
  16. return k;
  17. }
  18.  
  19. int cmp(int a[], int yy[], int cnt) //*****比较a,yy的大小
  20. {
  21. for(int i=; i<cnt; i++)
  22. {
  23. if(a[i]>yy[i]) return ;
  24. if(a[i]<yy[i]) return ;
  25. }
  26. return ;
  27. }
  28.  
  29. int main(void)
  30. {
  31. ll x, y, ans=;
  32. int a[MAXN], b[MAXN], aa[MAXN], bb[MAXN];
  33. cin >> x >> y;
  34. if(x==&&y==)
  35. {
  36. cout << "" << endl;
  37. return ;
  38. }
  39. if(x==) x++;
  40. int cnt1=get_erjinzhi(x, a);
  41. int cnt2=get_erjinzhi(y, b);
  42. for(int i=; i<cnt1; i++)
  43. {
  44. aa[i]=;
  45. }
  46. aa[]=;
  47. if(cmp(aa, a, cnt1))
  48. {
  49. if(cnt1==cnt2)
  50. {
  51. if(cmp(b, aa, cnt1))
  52. ans++;
  53. }
  54. else ans++;
  55. }
  56. for(int i=; i+<cnt1; i++)
  57. {
  58. swap(aa[i], aa[i+]);
  59. if(cmp(aa, a, cnt1))
  60. {
  61. if(cnt1==cnt2)
  62. {
  63. if(cmp(b, aa, cnt1)) ans++;
  64. }
  65. else ans++;
  66. }
  67. }
  68. for(int i=; i<cnt2; i++)
  69. {
  70. bb[i]=;
  71. }
  72. bb[]=;
  73. int k=;
  74. if(cnt1!=cnt2)
  75. {
  76. while(cmp(b, bb, cnt2)&&k<cnt2)
  77. {
  78. ans++;
  79. swap(bb[k], bb[k+]);
  80. if(cmp(b, bb, cnt2)==)
  81. {
  82. ans++;
  83. break;
  84. }
  85. k++;
  86. }
  87. }
  88. for(int i=cnt1+; i<cnt2; i++)
  89. {
  90. ans+=i-;
  91. }
  92. cout << ans << endl;
  93. return ;
  94. }

方法2:(二进制枚举,看了别人代码才想到的,再一次证明了我是个渣渣)

思路:

二进制运算的姿势一定要摆对,不然得做好久…
举个例子:
10000-1=1111
1111-1=1110
1111-10=1101
1111-100=1011

代码:

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. ll a, b, ans=;
  8. cin >> a >> b;
  9. for(int i=; i<=; i++)
  10. {
  11. for(int j=; j<i-; j++)
  12. {
  13. ll temp=(1ll<<i)--(1ll<<j);
  14. if(temp>=a && temp<=b) ans++;
  15. }
  16. }
  17. cout << ans << endl;
  18. return ;
  19. }

方法3:(dfs)

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. ll ans=, a, b;
  6.  
  7. void dfs(ll x, ll flag)
  8. {
  9. if(x>b) return;
  10. if(x>=a&&x<=b&&flag) ans++;
  11. if(flag==) dfs(x*, );
  12. dfs(x*+, flag);
  13. }
  14.  
  15. int main(void)
  16. {
  17. cin >> a >> b;
  18. dfs(, );
  19. cout << ans << endl;
  20. return ;
  21. }

Good Bye 2015B(模拟或者二进制枚举)的更多相关文章

  1. CUGBACM_Summer_Tranning1 二进制枚举+模拟+离散化

    整体感觉:这个组队赛收获还挺多的.自从期末考试以后已经有一个多月没有 做过组队赛了吧,可是这暑假第一次组队赛就找回了曾经的感觉.还挺不错的!继续努力!! 改进的地方:这次组队赛開始的时候题目比較难读懂 ...

  2. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  3. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  4. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  5. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

  6. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

  7. 南阳OJ-91-阶乘之和---二进制枚举(入门)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...

  8. 关于二进制枚举-计蒜客-得到整数X

    某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 X.求某君有多少种不同的方案来凑出整数 X. 输入格式 第一行,输入两个整数 n,X(1≤n≤2 ...

  9. BZOJ1688|二进制枚举子集| 状态压缩DP

    Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...

随机推荐

  1. 【转】MySQL数据类型和常用字段属性总结

    来源:http://www.jb51.net/article/55853.htm 这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. ...

  2. Tomcat7优化配置

    导读 Tomcat在使用的过程中会遇到很多报错,有些是程序的报错,但还有一部分是tomcat本身的报错,我们可以通过优化tomcat的初始配置来提高tomcat的性能.Tomcat的优化主要体现在两方 ...

  3. 写给喜欢用Block的朋友(ios Block)

    作者:fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/38090205转载请注明出处如果觉得文章对你有所帮助,请通过留言或关注 ...

  4. Understand:高效代码静态分析神器详解(转)

    之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...

  5. 2016移动端web5分钟速成(适合新手)

    http://www.w3cfuns.com/notes/20813/fecbb840a2574cf712a8625f88a7ab3a.html

  6. 剑指Offer 二维数组中的查找

    题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路法一: * 矩阵是 ...

  7. OpenCv高斯,中值,均值,双边滤波

    #include "cv.h" #include "highgui.h" #include <iostream> using namespace s ...

  8. 怎样将runlmbench 获取的数值传给上层app

    前面那个随笔 , 已经成功将runlmbench 移植到了Android , 并成功的运行. 今天就写一下将runlmbench 获取的那些性能值传给上层 App 进行人机交互. 一开始 , 我是想直 ...

  9. PHP判断用户操作系统(Android,ipad,iphone,windows)

    这段脚本可以运用在:针对不同的操作系统,把用户引导向相应的网站或做相应的处理. <?php // PHP 判断客户端平台(PC.安卓.iPhone.平板) // strpos() 函数返回字符串 ...

  10. json_decode()和json_encode()的使用方法

    json_decode对JSON格式的字符串进行编码 json_encode对变量进行 JSON 编码 JS中对JSON的解析 一.JSON字符串转换为JSON对象     要运用上面的str1,必须 ...