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
题意:给出两个数l和r,问在这两个数之间的数谁的二进制表示中1的个数最多,如果有相同多的个数,则输出最小的那个数。
思路1:每一次都把最低位的0变为1,判断是否小于r,这个技巧比较难想。
思路2:从最低位开始与1相或,判断是否小于r
AC1:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
long long l,r;
long long ans;
cin>>l>>r;
while(l<=r)
{
ans=l;
long long m=(~l)&(-(~l));
l+=m;
}
cout<<ans<<endl;
}
return 0;
}
AC2:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
long long l,r;
cin>>l>>r;
long long x=1;
while(1)
{
if((l|x)<=r)
l=(l|x);
else
break;
x<<=1;
}
cout<<l<<endl;
}
return 0;
}
另外延伸一种把最低位的1变为0的方法:n&(n-1)
例如n = 10100,则(n-1) = 10011 n&(n-1) = 10000