Codeforces Round #276 (Div. 1) A. Bits Greedy

Source: Internet
Author: User

A. Bits

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- lxR, and is maximum Possibl E. If there is 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 II integers li, ri -t He arguments for the corresponding query (0≤ liri ≤101 8).

Output

For each query, print the answer in a separate line.

Sample Test (s) input
3
1 2
2 4
1 10
Output
1
3
7
Note

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

1= 12

2=2

3=2

4=2

5= 1012

6=2

7= 1112

8=2

9= 10012

Ten= 10102

Test Instructions : To n inquiries, each time you ask the number between l,r, in the binary under the 1-digit number of which is the most

Solution: We build from the L, in 0-bit, greedy from small to large-bit, must be the best

///1085422276#include<bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")using namespacestd; typedefLong Longll;typedef unsignedLong Longull;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_backinline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){        if(ch=='-') f=-1; ch=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************Const intn=6000+ -;#defineMoD 10000007#defineINF 1000000001#defineMAXN 10000intd[ the];ll Test (ll X,ll y) { for(intI=0;i< the; i++) {        if(! (x& (1ll<<i)) &&x+ (1ll<<i) <=y) x+ = (1ll<<i); }    returnx;}intMain () {intn=read (); ll L,r;  for(intI=1; i<=n;i++) {cin>>l>>R; cout<<test (l,r) <<Endl; }    return 0;}
Code

Codeforces Round #276 (Div. 1) A. Bits Greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.