题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多。

写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个位必须也相同。而rr在pos和pos后如果都为1,那么pos和pos后都取1,否则pos取0,pos后取1。

乱码:

  1. //#pragma comment(linker,"/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<string>
  5. #include<cstring>
  6. #include<vector>
  7. #include<cmath>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<set>
  12. #include<algorithm>
  13. #include <stack>
  14. #include <list>
  15. using namespace std;
  16. const int SZ=,INF=0x7FFFFFFF;
  17. typedef long long lon;
  18. const double EPS=1e-;
  19. const lon one=;
  20.  
  21. lon geth(lon x)
  22. {
  23. lon res=;
  24. for(lon i=;i<;++i)
  25. {
  26. if(x&(one<<i))
  27. {
  28. res=max(res,i);
  29. }
  30. }
  31. return res;
  32. }
  33.  
  34. int main()
  35. {
  36. std::ios::sync_with_stdio();
  37. //freopen("d:\\1.txt","r",stdin);
  38. lon n;
  39. cin>>n;
  40. for(int i=;i<n;++i)
  41. {
  42. lon ll,rr;
  43. cin>>ll>>rr;
  44. lon bit=geth(rr);
  45. lon res=;
  46. for(lon j=bit;j>=;--j)
  47. {
  48. lon b1=rr&(one<<j);
  49. lon b2=ll&(one<<j);
  50. if(b1^b2)
  51. {
  52. lon cand=;
  53. for(lon k=j;k>=;--k)
  54. {
  55. if(rr&(one<<k))++cand;
  56. }
  57. res+=(one<<max(j,cand))-;
  58. break;
  59. }
  60. else res+=((one<<j)&rr);
  61. }
  62. cout<<res<<endl;
  63. }
  64.  
  65. return ;
  66. }

codeforces 484a//Bits// Codeforces Round #276(Div. 1)的更多相关文章

  1. 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 ...

  2. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  3. C. Bits (Codeforces Round #276 (Div. 2) )

    题目大意:给你两个数l,r(l<r),求一个数是大于等于l且小于等于r的数中二进制数的1的个数最多,如果1的个数相同则取最小的那个(翻译渣,请见谅!) 思路:把左区间L化为二进制,再把左区间的二 ...

  4. Codeforces Round #276 (Div. 1) A. Bits 贪心

    A. Bits   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non ...

  5. Codeforces Round #276 (Div. 2)C. Bits(构造法)

    这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...

  6. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  7. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  8. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  9. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

随机推荐

  1. kafka存储数据量过大,导致磁盘爆满

    问题: 注意到自己负责kafka的某个topic最小的偏移量为0,而最大的偏移量都7亿多了,说明存储在kafka里面的数据没有定时删除,通过登陆到kafka服务器,查看配置文件services.pro ...

  2. shell字符串操作技巧

    操作字符串 -------------- Bash支持超多的字符串操作,操作的种类和数量令人惊异.但不幸的是,这些工具缺乏集中性. 一些是参数替换的子集,但是另一些则属于UNIX的expr命令.这就导 ...

  3. Servlet过滤器和监听器配置范例

    1,Servlet过滤器 <filter> <filter-name>charset</filter-name> <filter-class>org.g ...

  4. Quartz框架调用Demo

    Quartz框架调用Demo 任务调度在JAVA应用程序中运用的十分普遍,掌握QUARTZ是必备的技能; 官网:http://www.quartz-scheduler.org/ 下载最新1.80资源包 ...

  5. 安装webpack出现警告: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):

    警告如下: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_mo ...

  6. Starting MySQL...The server quit without updating PID file [失败]local/mysql/data/localhost.localdomain.pid报错

    在添加命令自动补全的时候mysql启动失败 这是原配 # For advice on how to change settings please see # http://dev.mysql.com/ ...

  7. 20145330 《网络对抗》 Eternalblue(MS17-010)漏洞复现与S2-045漏洞的利用及修复

    20145330 <网络对抗> Eternalblue(MS17-010)漏洞利用工具实现Win 7系统入侵与S2-045漏洞的利用及修复 加分项目: PC平台逆向破解:注入shellco ...

  8. 0x30、0x37

    1.write_date(0x30+shi)加0x30是什么意思 答: 将数字0-9转化为字符'0'-'9' 1.write_date(0x37+bai)加0x37是什么意思 答: 将大于9的数字转化 ...

  9. js实现ajax请求

    练下手,好久没写ajax var xmlhttp=null;//创建XMLHttprequest function createXMLHttpRequest(){ if(window.ActiveXO ...

  10. Linxu内核版本号后面多出字符串或者+号【学习笔记】

    作者:庄泽彬 之前一直没有留意到但是最近在编译内核的时候版本号竟然多出了个加号+号或字符串, 后面终于找到原因了,原来config如果设置了CONFIG_LOCALVERSION_AUTO=y,内核的 ...