题目链接:

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. 1秒30000QPS,前后端设计思路

    Q:现在有这样一个需求,在一秒中有3万的支付订单请求,有什么比较好的解决方案吗? PS:我们数据库用的是oracle 程序是java spring mybatis dubbo mq等技术,现在有这样一 ...

  2. js中的浅拷贝和深拷贝

    说说最近所学:浅拷贝和深拷贝也叫做浅克隆和深克隆,深浅主要针对的是对象的"深度",常见的对象都是"浅"的,也就是对象里的属性就是单个的属性,而"深&q ...

  3. Tomcat一些小事

    1.编码问题 1.1.乱码 客户端发请GET请求,如果这个请求地址上有中文,而且也没有进行encode的时候,后端就可能接收到乱码. --解决办法 在tomcat , conf/server.xml ...

  4. 使用一般处理程序HTTPHandler下载文件

    一般来说我们可以用HTTPHandler来处理一些简单的逻辑,比如验证码.下载文件等. 以下载word文档为例讲解一下如何在HHTPHandler中下载文件,不限于word文档,如果下载其他文件,需要 ...

  5. Fix Internet Explorer Crashes with SharePoint 2013 Online Presence Indicators

    IE中,只要是鼠标浮动到人名字上面的状态的时候,这个状态是与Lync相连接的,IE就会出现停止工作. 以下是解决方法. Until the other day when I figured this ...

  6. 详解Paint的setMaskFilter(MaskFilter maskfilter)

    一.setMaskFilter(MaskFilter maskfilter) setMaskFilter(MaskFilter maskfilter)是paint中的方法,它可以用来对图像进行一定的处 ...

  7. 适配iPhone6和iPhone6 Plus

    先对比所有市面上的iPhone设备,然后分析如何适配新的设备,   iPhone4,iPhone4s 分辨率960*640  长宽比1.5iPhone5,iPhone5s  分辨率1136*640   ...

  8. Universal-Image-Loader完全解析(上)

    Universal-Image-Loader完全解析(上) 基本介绍及使用 大家平时做项目的时候,或多或少都会接触到异步加载图片,或者大量加载图片的问题,而加载图片时候经常会遇到各种问题,如oom,图 ...

  9. vi编辑器常用配置

    在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc. 在启动vim时,当前用户 ...

  10. 【读书笔记】iOS-NSNumber

    NSArray和NSDictionary只能存储对象,而不能直接存储任何基本类型的数据,如int,float或struct.但是你可以用对象来封装基本数值.例如,将int型数据封装到一个对象中,然后就 ...