D. Little Girl and Maximum XOR (greedy)

Source: Internet
Author: User

D. Little Girl and Maximum XOR

A little girl loves problems on bitwise operations very much. Here ' s one of them.

You are given the integers l and R. Let's consider the values of for all pairs of integersa and b(l? ≤? A? ≤? b? ≤? r). Your task is to find the maximum value among all considered ones.

Expression means applying bitwise excluding or operation to integersx and y. The given operation exists in all modern programming languages, for example, in languagesC+ + and Java it is represented as "^", inPascal -as? xor?.

Input

The single line contains space-separated integersl and R (1?≤? L? ≤? r≤?10).

%lld specifier to read or write 64-bit integers inс++. It is preferred to use thecin, cout streams or the%i64d specifier.

Output

In a single line print a single integer-the maximum value of pairs of integersa, b(l ? ≤? a? ≤? b? ≤? r).

Sample Test (s)Input
1 2
Output
3
Input
8 16
Output
31
Input
1 1
Output
0 Test instructions: In the interval [l,r] Find two number A, a (a <= b), so that a ^ B to reach the maximum; solution: (1) First there is such a phenomenon, when the 2 ^ n <= r of N reaches the maximum, and 2 ^ n-1 >= L, the answer is (2 ^ n + 2 ^ n-1), that is, the second binary number is all one, and the maximum value of XOR, can not be large, because 2 ^ n has reached the limit; Another case is greedy when 2^n < L. From high to low enumeration, for the current if the binary number is 1, smaller than L, then set to 1, if the current value is greater than R, then set to 0; Otherwise, the following bits meet (1); AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <bitset > #include <algorithm> #include <vector> #define Lson l,mid,u << 1#define rson mid + 1,r,u << 1 | 1#define ls u << 1#define rs u << 1 |    1#define Exp 1e-8using namespace Std;typedef long long ll;int main () {ll l,r;        while (Cin>>l>>r) {ll maxn = 0;            if (L = = r) {puts ("0");        Continue        } ll i = 0,ans;            while (true) {ans = 1LL << i;                if (Ans > R) {ans = 1LL << (i-1);            Break        } i++;        } i--;        if (Ans >= l && ans-1 >= l) {Cout<<ans + ans-1<<endl;                } else {while (true) {ll res = ans + (1LL << (i-1));                if (res <= l) ans + = 1LL << (i-1); else if (res <= r) {ans = 0;                    for (ll j = 0; J < i; J + +) {ans + = 1LL << J;                } break;            } i--;        } cout<<ans<<endl; }} return 0;}


D. Little Girl and Maximum XOR (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.