题目链接:

A. Bits

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Examples
input
3
1 2
2 4
1 10
output
1
3
7 题意: 在[l,r]中找一个数字使得这个数的二进制的1数目最多,如果有多种答案就输出最小的那个; 思路: 跟上次一道hdu的题目相似,按二进制位贪心,并更新l和r,如果当前为一个为0一个为1,那么就知道后面为都可以取1,还要判断这一位是否可取1,如果都是0那么就下一位,如果都是1那么就更新答案和l,r再进行下一位; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+20;
const int maxn=4e3+220;
const double eps=1e-12;
int n;
LL ans=0;
LL solve(LL l,LL r,int pos)
{ int tl=((l>>pos)&1),tr=((r>>pos)&1);//cout<<l<<" "<<r<<" "<<pos<<" "<<tl<<" "<<tr<<endl;
if(tl==0&&tr==1)
{
if(pos==0)ans+=1;
else
{
if(r!=(1LL<<(pos+1))-1)ans+=(1LL<<pos)-1;
else ans+=r;
}
}
else if(tl==0&&tr==0&&pos)solve(l,r,pos-1);
else if(tl==1&&tr==1)
{
ans+=(1LL<<pos);
if(pos)solve(l-(1LL<<pos),r-(1LL<<pos),pos-1);
}
}
int main()
{
read(n);
LL l,r;
For(i,1,n)
{
ans=0;
read(l);read(r);
solve(l,r,63);
print(ans);
} return 0;
}

  

codeforces 484A A. Bits(贪心)的更多相关文章

  1. 【Codeforces 484A】Bits

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

  2. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  3. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  4. CodeForces484A Bits(贪心)

    CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...

  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   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non ...

  7. CodeForces 484A Bits(水题)

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

  8. Codeforces 484A - Bits 二进制找1

    这题可以根据l, r 在二进制下的长度进行分类. l  的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为  (1LL << (r ...

  9. CodeForces 484A Bits

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

随机推荐

  1. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  2. 一、PBNI环境搭建及初步使用

    PowerBuilder Native Interface(PowerBuilder本机接口PBNI)允许将第3方程序转换为PowerBuilder对象,供PowerBuilder直接使用,也允许将P ...

  3. 阿里云主机上安装jdk

    今天继续安装jdk到阿里云服务上,大家要看一下阿里云是32位还是64位的,如果是32位下载32位的包,如果是64位的下载64位的包 我的就是64位的,开始我还不知道是怎么区分32/64位的,原来X64 ...

  4. 常用Keytool 命令

    常用Keytool 命令Keytool 是一个JAVA环境下的安全钥匙与证书的管理工具.它管理一个存储了私有钥匙和验证相应公共钥匙的与它们相关联的X.509 证书链的keystore(相当一个数据库, ...

  5. KindEditor编辑器在ASP.NET中的使用

    KindEditor编辑器在ASP.NET中的使用 最近做的项目中都有用到富文本编辑器,一直在寻找最后用的富文本编辑器,之前用过CKEditor,也用过UEditor,这次打算用 一下KindEdit ...

  6. 安装和配置tomcat服务器

    本文主要介绍一下tomcat服务器的安装和配置 1.获取tomcat tomcat服务器可以到它的官方网站(http://tomcat.apache.org)上下载 2.安装tomcat 具体步骤: ...

  7. bash shell命令(2)

    在上篇<bash shell命令(1)>中,介绍了几种简单的linux shell命令,今天继续介绍bash shell命令 本文地址:http://www.cnblogs.com/arc ...

  8. Android常用设计模式(一)

    java有23中设计模式,Android中也用到了很多的设计模式,本篇就来介绍Android中常用的几种设计模式 一.普通工厂设计模式 普通工厂设计模式,就是创建一个工厂类负责创建对象 ,用户根据需求 ...

  9. [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案

    在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...

  10. 每日Scrum--No.5

    Yesterday:学习并编写代码 Today:组织小组开一次阶段性的总结会议:讨论需求分析中存在的问题:继续学习和编写代码:总结前阶段代码出现的问题 Problem:编程要注意很多的特殊情况,程序成 ...