Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)

D. Little Girl and Maximum XOR
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

You are given two integers l and r. Let's consider the values of for all pairs of integers a 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 integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as "^", in Pascal — as «xor».

Input

The single line contains space-separated integers l and r (1 ≤ l ≤ r ≤ 1018).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

In a single line print a single integer — the maximum value of for all pairs of integers a, b (l ≤ a ≤ b ≤ r).

Sample test(s)
Input
1 2
Output
3
Input
8 16
Output
31
Input
1 1
Output
0

思路简述:对于l和r,先化为对应的二进制数,自高位向下逐位比较,不同不需变化,相同则若同为1,r的此位变为0(如果是l的此位变为0会
使得得到的数小于l),同为0则l的此位变为1(若r的此位变为1,则得到的数大于r)。
 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <vector>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <cstdio>
10 #include <cstring>
11 #include <algorithm>
12 using namespace std;
13 #define LL long long
14 #define cti const int
15 #define ctll const long long
16 #define dg(i) cout << "*" << i << endl;
17 
18 LL l, r;
19 char sl[65], sr[65];
20 
21 int NumToStr(char *s, LL x)
22 {
23     int i;
24     for(i = 0; x != 1; i++)
25     {
26         s[i] = (x & 1) + '0';
27         x >>= 1;
28     }
29     s[i] = '1';
30     return i;
31 }
32 
33 LL StrToNum(char *s)
34 {
35     LL x = 0;
36     int i, j;
37     for(i = 63; s[i] == '0'; i--){}
38     for(j = 0; j <= i; j++)
39         if(s[j] == '1') x += ((LL)1 << j);
40     return x;
41 }
42 
43 int main()
44 {
45     int i, j;
46     while(scanf("%I64d %I64d", &l, &r) != EOF)
47     {
48         if(l == r)
49         {
50             puts("0");
51             continue ;
52         }
53         memset(sl, '0', sizeof(sl));
54         memset(sr, '0', sizeof(sr));
55         int lenl = NumToStr(sl, l);
56         int lenr = NumToStr(sr, r);
57         int tag;
58         if(lenr > lenl) tag = 1;
59         else tag = 0;
60         for(i = lenr - 1; tag != 2 && i >= 0; i--)
61         {
62             if(sr[i] > sl[i]) tag = 1;
63             if(tag && sr[i] == sl[i]) tag = 2;
64         }
65         if(tag != 2) printf("%I64d\n", l^r);
66         else
67         {
68             for(j = i + 1; j >= 0; j--)
69             {
70                 if(sl[j] == sr[j])
71                 {
72                     if(sl[j] == '1') sr[j] = '0';
73                     else sl[j] = '1';
74                 }
75             }
76             printf("%I64d\n", StrToNum(sl)^StrToNum(sr));
77         }
78     }
79     return 0;
80 }

 


posted on 2013-03-02 12:16  铁树银花  阅读(334)  评论(0编辑  收藏  举报

导航