这题可以根据l, r 在二进制下的长度进行分类。

l  的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r ;

一种是取答案为  (1LL << (rcnt - 1)) - 1 ,意思为比 r 小一位长度,也是 1* 这种样子的数。

l 的长度等于 r 的时候,答案从 l 开始找 , 按位 与 1,同时要满足答案不大于 r  即可。

source code (有参考):

  1. //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <stack>
  7. #include <queue>
  8. #include <vector>
  9. #include <algorithm>
  10. #define ll long long
  11. #define Max(a,b) (((a) > (b)) ? (a) : (b))
  12. #define Min(a,b) (((a) < (b)) ? (a) : (b))
  13. #define Abs(x) (((x) > 0) ? (x) : (-(x)))
  14. using namespace std;
  15.  
  16. const int INF = 0x3f3f3f3f;
  17.  
  18. int Cal(long long x){
  19. int ret = ;
  20. while (x){
  21. ++ret;
  22. x >>= ;
  23. }
  24. return ret;
  25. }
  26.  
  27. int main(){
  28. int n;
  29. long long l, r, ret;
  30. cin >> n;
  31. while (n--){
  32. cin >> l >> r;
  33. int lcnt = Cal(l);
  34. int rcnt = Cal(r);
  35. if (lcnt < rcnt){ //for lcnt != rcnt, answer must like 1* to r
  36. if (r == (1LL << rcnt) - ){
  37. ret = r;
  38. }
  39. else{
  40. ret = (1LL << (rcnt - )) - ;
  41. }
  42. }
  43. else{ //for lcnt == rcnt, answer must be 1* and less than r
  44. for (int i = ; i < rcnt; ++i){
  45. if (((1LL << i) | l) <= r){
  46. l |= (1LL << i);
  47. }
  48. }
  49. ret = l;
  50. }
  51. cout << ret << endl;
  52. }
  53. return ;
  54. }

Codeforces 484A - Bits 二进制找1的更多相关文章

  1. CodeForces 484A Bits(水题)

    A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  2. codeforces 484a//Bits// Codeforces Round #276(Div. 1)

    题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...

  3. CodeForces 484A Bits

    意甲冠军: 10000询价  每次查询输入L和R(10^18)  在区间的二进制输出指示1大多数数字  1个数同样输出最小的 思路: YY一下  认为后几位全是1的时候能保证1的个数多  那么怎样构造 ...

  4. codeforces 484A A. Bits(贪心)

    题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  6. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  7. 【Codeforces 484A】Bits

    [链接] 我是链接,点我呀:) [题意] 让你求出l~r当中二进制表示1的个数最多的数x [题解] 最多有64位 我们可以从l开始一直增大到r 怎么增大? 找到l的二进制表示当中0所在的位置 假设i这 ...

  8. [ 9.22 ]CF每日一题系列—— 484A Bits

    Description: 给你一个l,r的区间让你找一个最小的x并且其二进制数要包含最多的1位,输出它的十进制 Solution: 我本来就是贪心,但是贪大了,想1一直往上添加1,但是忘记了0在中间的 ...

  9. Codeforces - 1020B Badge(找环)

    题意: 以每个点为起点,找到第一个出现两次的点 解析: 我是先找出来所有的环  环上的点找出来的肯定是自己 bz[i]  = i; 然后去遍历不在环上的点j  如果通过这个点找到一个已经标记的的点i ...

随机推荐

  1. IOS 学习笔记(1) 视图UIViewController

    1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...

  2. Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED

    Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED Xvfb+YSlow+ShowSlow搭建前端性能测试框架 作者:黑三 | 时间:2010-07-07 ...

  3. Html.ActionLink(转载)

    @Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index" ...

  4. No.3小白的HTML+CSS心得篇

    A--看的东西多了总会出现好多模糊不清的又长的很像的的词语 今天对此进行区别分析下 1. align 与 text-align的区别 align 在W3Cschool中是这样解释的  ----alig ...

  5. 使用命令部署wsp包,并将其部署到不同的web应用程序

    http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-deploy-a-wsp-using-powershell-in-sharepoint- ...

  6. 【QT相关】文件、目录基础操作

    判断目录是否存在: QString proFile(t_path); proFile.append("/dir"); QFileInfo proFileInfo(proFile); ...

  7. JavaSE学习总结第19天_IO流1

      19.01  集合的特点和数据结构总结 HashSet.HashMap.Hashtable判断元素唯一性的方式: 通过对象的hashCode和equals方法来完成元素唯一性 如果对象的hashC ...

  8. div+css 布局下兼容IE6 IE7 FF常见问题

    div+css 布局下兼容IE6 IE7 FF常见问题 收藏 所有浏览器 通用 (市面上主要用到的IE6 IE7 FF)height: 100px; IE6 专用 _height: 100px; IE ...

  9. centos主机建立ssh互信

    ssh-keygen 生成密钥 1.ssh-keygen -t rsa 可以加密和签名 rsa 只能加密不能签名 2.ssh-copy-id -i /root/.ssh/id_rsa.pub USER ...

  10. Dapper 多数据库优化

    Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...