这题可以根据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. The introduction to Web.config of ASP.NET #Reprinted#

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  2. 射频识别技术漫谈(11)——Mifare系列卡的共性

    Mifare是NXP公司生产的一系列遵守ISO14443A标准的射频卡,包Mifare S50.Mifare S70.Mifare UltraLight.Mifare Pro.Mifare Desfi ...

  3. WINDOWS硬件通知应用程序的常方法

    摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口.为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,并 ...

  4. 滚动条QScroolBar实现滚屏功能(屏幕过大,覆盖wheelEvent来处理滑轮事件)

    环境:Qt5 编译器:Qt Creator 需求:如图 显示区域win 600*300 需要显示的Widget控件show 590*550 则有600*250的show界面无法显示 使用滑块控制sho ...

  5. SqlServer2012导入Oracle详细案例

    第一次使用SqlServer2012,界面和VS2012风格一致了,Great! 进入正题,这篇博文主要写一下自己亲测的一个案例,使用SqlServer2012的“导入和导出数据”功能向Oracle1 ...

  6. Opencv 学习资料集合(更新中。。。)

    基础学习笔记之opencv(24):imwrite函数的使用 tornadomeet 2012-12-26 16:36 阅读:13258 评论:9 基础学习笔记之opencv(23):OpenCV坐标 ...

  7. 一些常用的Intent及intent-filter的信息

    Uri Action 功能 备注 geo:latitude,longitude Intent.ACTION_VIEW 打开地图应用程序并显示指定的经纬度   geo:0,0?q=street+addr ...

  8. return view详解(转载)

    1.return View(); 返回值 类型:System.Web.Mvc.ViewResult将视图呈现给响应的 View() 结果. 注释 View() 类的此方法重载将返回一个具有空 View ...

  9. SAP HANA 开发者中心(Developer Center)入门指南

  10. HTTP BIN测试

    http://httpbin.org/ Tracing XML request/responses with JAX-WS: http://stackoverflow.com/questions/19 ...