codeforces484A

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=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('
')
int Q;
bool Arr[65];
//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=0,oo=-1;
        for(i=0;ans<R;i++)
        {
            ++oo;
            ans+=(1ll<<(oo));
        }
        memset(Arr,0,sizeof Arr);
        while(ans>R)
        {
            for(j=oo;~j;j--) if(!Arr[j])
            {
                if(ans-(1ll<<j)>=L)
                {
                    ans-=(1ll<<j); Arr[j]=1; break;
                }
            }
        }
        Wl(ans);
    }
    return 0;
}
/*
Input
3
1 2
2 4
1 10
Output
1
3
7
*/
View Code
原文地址:https://www.cnblogs.com/gaojunonly1/p/10737783.html