码迷,mamicode.com
首页 > 其他好文 > 详细

CQUOJ 9906 Little Girl and Maximum XOR

时间:2016-04-22 01:08:20      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

Little Girl and Maximum XOR

Time Limit: 2000ms
Memory Limit: 262144KB
This problem will be judged on CodeForces. Original ID: 276D
64-bit integer IO format: %I64d      Java class name: Any
 
 

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 Input

Input
1 2
Output
3
Input
8 16
Output
31
Input
1 1
Output
0
 1 /*
 2 2016年4月22日00:05:10
 3     ^ 为异或运算 
 4 经过打表可得规律答案要么是0 要么是2的N次 - 1 
 5 
 6 要得到最大的XOR值,其值一定是2的N次 - 1
 7 
 8 即在l 和 r的二进制中,从左到右遍历过去如果碰到 (2 ^ i)&l 为 1 , (2 ^ i)&r 为 0
 9 即在 l 和 r 之间一定存在 形如 011111111 和 100000000 这样的数。
10 
11 则可说明在[l , r]中存在 011111111111 和 10000000000 可得到最大XOR值为2的N次 - 1
12 
13 PS:不会存在首先出现 l 为 0 r 为 1 的情况,因为 l < r 
14 */
15 
16 # include <iostream>
17 # include <cstdio>
18 # include <cstring>
19 # include <algorithm>
20 # include <queue>
21 # include <vector>
22 # include <cmath>
23 # define INF 0x3f3f3f3f
24 using namespace std;
25 
26 int main(void)
27 {
28     int i;
29     long long l, r, ans;
30     while (cin>>l>>r){
31         // 1<<i 相当于 2的i次方 
32         // 1000 0000 0 第9位 2的8次方  1<<8 
33         // 所以第64位 2的63次方  即 1<<63 
34         for (i = 63; i >= 0; i--){
35             if ((l&(1LL<<i)) ^ (r&(1LL<<i))){ // 1为int类型 要转为LL 
36                 break;
37             }
38         }
39         ans = (1LL<<(i+1)) - 1; // 要在原来位数上加一位  然后减1 
40         cout<<ans<<endl;
41     }
42     
43     return 0;    
44 }

 

CQUOJ 9906 Little Girl and Maximum XOR

标签:

原文地址:http://www.cnblogs.com/hyq123456/p/5419528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!