您的位置:首页 > 其它

codeforces C. Bits 贪心

2016-05-18 23:19 369 查看
C. 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


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, 求l和r之间的数中二进制中1的个数最多的数是哪一个, 如果有多个答案, 输出最小的那个数.

分析: 我们可以贪心的一步一步的将l的最低位0 变成1, 因为要使1的个数最多, 直到l再取大于r的时候就不能取了,这时候l的二进制中1的数目最多, 并且是最小的.

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int N=100010,MOD=1e9+7;

int main()
{
int n;
cin>>n;
while(n--)
{
ll l,r;
cin>>l>>r;
ll ans=l;
while(l<=r)
{
ans = l;
for(int i=0;i<61;i++){
if(!(l&(1LL<<i))){
l |= 1LL<<i;
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}


#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int N=100010,MOD=1e9+7;

int main()
{
int n;
cin>>n;
while(n--)
{
ll l,r;
cin>>l>>r;
ll ans=l;
while(l<=r)
{
ans = l;
ll m = (~l)&(-(~l));
l += m;
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: