题目链接:

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. Cache&Session Viewer

    用于查看和删除网站Cache https://github.com/sdf333/Aspy

  2. 管理系统的前端解决方案:Pagurian V1.3发布

    Pagurian 一个管理系统的前端解决方案, 致力于让前端设计,开发,测试,发布更简单. 功能简介 Pagurian 适用于Web管理级的项目 基于Sea.js遵循CMD规范,友好的模块定义,使业务 ...

  3. css权重是什么

    css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...

  4. JQuery EasyUI Tree

    Tree 数据转换 所有节点都包含以下属性: id:节点id,这个很重要到加载远程服务器数据 which is important to load remote data text: 显示的节点文本 ...

  5. thinkPHP学习笔记(2)

    1.调试模式 设置调试模式部分代码如下: <?php define('APP_DEBUG',TRUE); // 开启调试模式 常量定义代码 require '/ThinkPHP框架所在目录/Th ...

  6. oracle断电重启之ORA-01033和ORA-01172

    参考文献: ORA-01033:解决方法 数据库掉电后 ORA-01172 磁盘坏块解决方法 --尝试连接数据库prjdb C:\Documents and Settings\Administrato ...

  7. android webview 介绍

    在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装成名为WebView的组件. WebView使用: (1)添加权限:AndroidManifest.xml中必须使用许可&q ...

  8. IOS 网络浅析(一 网络监测~Reachability)

    网络监测应用于各种需要连接网络的app设计,由于现在开发的app几乎都用到网络,因此,网络监测也成为了较为重点的知识,下面我给大家简单讲解一下网络监测的实际应用,依旧会有代码哦. 想要实现网络监测,可 ...

  9. GCD中的dispatch_apply的用法及作用

    GCD中的dispatch_apply的用法及作用 (一)dispatch_apply的基本用法 dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联A ...

  10. Runtime(动态添加属性)

    下面通过一个实例展示一下Runtime(动态添加属性)的用法 下面对运行时添加属性用到的策略参数进行补充: 这样看来,前面的NSString* name用的策略是retain nonatomic就知道 ...