Bits

CodeForces - 484A

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

Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102

sol:因为二进制一些奇奇怪怪的东西,我们可以得到以下的乱搞,先一位位加上去加到比R大了为止,然后要减到恰好大于等于L,所以尽可能减大的,并没有减过的位

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
int Q;
bool Arr[];
//const ll a[65]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215,33554431,67108863,134217727,268435455,536870911,1073741823,2147483647,4294967295,8589934591,17179869183,34359738367,68719476735,137438953471,274877906943,549755813887,1099511627775,2199023255551,4398046511103,8796093022207,17592186044415,35184372088831,70368744177663,140737488355327,281474976710655,562949953421311,1125899906842623,2251799813685247,4503599627370495,9007199254740991,18014398509481983,36028797018963967,72057594037927935,144115188075855871,288230376151711743,576460752303423487,1152921504606846975,2305843009213693951};
int main()
{
int i,j;;
R(Q);
while(Q--)
{
ll L=read(),R=read(),ans=,oo=-;
for(i=;ans<R;i++)
{
++oo;
ans+=(1ll<<(oo));
}
memset(Arr,,sizeof Arr);
while(ans>R)
{
for(j=oo;~j;j--) if(!Arr[j])
{
if(ans-(1ll<<j)>=L)
{
ans-=(1ll<<j); Arr[j]=; break;
}
}
}
Wl(ans);
}
return ;
}
/*
Input
3
1 2
2 4
1 10
Output
1
3
7
*/

codeforces484A的更多相关文章

  1. CodeForces484A Bits(贪心)

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

  2. CodeForces484A——Bits(贪心算法)

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

随机推荐

  1. Generative Adversarial Nets[content]

    0. Introduction 基于纳什平衡,零和游戏,最大最小策略等角度来作为GAN的引言 1. GAN GAN开山之作 图1.1 GAN的判别器和生成器的结构图及loss 2. Condition ...

  2. [Flume][Kafka]Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic)

    Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic) 进行准备工作: $sudo mkdir -p /flume/web_spooldir$su ...

  3. python事物管理及同步锁

    我们经常会遇到这样子的问题,我给朋友赚钱100,分为两步: 1)我的账户-100 2)朋友账户 +100 看似需求很简单,但是如果在上面的步骤1)结束后,系统崩溃了怎么办? 数据库中有事物管理,也就是 ...

  4. 美团2016秋招笔试B

    1.下述解决死锁的方法中,属于死锁预防策略的是? 资源有序分配法  银行家算法:避免死锁 资源有序分配法:预防死锁 资源分配图化简法:检测死锁 撤销进程法:解决死锁   2. 什么是死锁? 如果一个进 ...

  5. 使用publisher模式控制频繁的UI输出,避免Winform界面假死

    http://www.cnblogs.com/Charltsing/p/publisher.html 最近测试task并发任务的效率与线程池的区别,发现了另外一个问题.task建立任务的速度很快,输出 ...

  6. os.path 下的各方法

    一.os.path os.path.abspath(file) #拿到当前程序(文件)的绝对目录. os.path.split(pathname) # 返回一个元组,第零个元素为文件上级绝对目录,第一 ...

  7. Shell脚本2

      5 Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数, 脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… ...

  8. CentOS的el5, el6, el7代表什么

    https://www.cnblogs.com/EasonJim/p/9051851.html el: enterprise linux?

  9. python3 selenium webdriver 元素定位xpath定位骚操作

    源文http://www.cnblogs.com/qingchunjun/p/4208159.html By.xpath() 这个方法是非常强大的元素查找方式,使用这种方法几乎可以定位到页面上的任意元 ...

  10. react购物车demo

    import React, { Component } from 'react'; import './App.css'; import {connect} from 'react-redux'; i ...