经过打表可得规律答案要么是0 要么是2的N次
要得到最大的XOR值,其值一定是2的N次
即在 l 和 r 的二进制中,从左到右遍历过去,如果碰到 l 为 1 r 为 0
则可说明在『l , r】中存在 1000000000 和 0111111111 可得到最大XOR值为2的N次
PS:不会存在首先出现 l 为 0 r 为 1 的情况,因为 l < r
#include<stdio.h> #include<math.h> int main(){ long long x,y,ans=0; int i; while(EOF != scanf("%lld%lld",&x,&y)){ ans = 0; for(i=63;i>=0;i--) if((x&((long long)1<<i))^(y&((long long)1<<i))) break; ans = pow(2,i+1)-1; printf("%lld\n",ans); } return 0; }
题目描述
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?.
输入
The input consists of multiple test cases.
Each test case contains space-separated integers l and r (1?≤?l?≤?r?≤?1018).
输出
In a single line print a single integer — the maximum value of for all pairs of integers a, b (l?≤?a?≤?b?≤?r).