Codeforces 276D. Little Girl and Maximum XOR(模拟)

ゞ 浴缸里的玫瑰 2022-06-07 13:16 359阅读 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 4044ca87d9d7298f84b83138e9d1049c_v_1508041262 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 b6c25253add7f19654760f9ea40fe9c0_v_1508041262 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 4044ca87d9d7298f84b83138e9d1049c_v_1508041262 for all pairs of integers a, b (l ≤ a ≤ b ≤ r).

Example

Input

  1. 1 2

Output

  1. 3

Input

  1. 8 16

Output

  1. 31

Input

  1. 1 1

Output

  1. 0

题解:

看起来很难其实想清楚了就很水的一题,就是求数组x,y之间的数异或的最大值

思路:

最后找规律可以发现只要把x,y都转化为二进制,从高位往低位找到第一个“1 0”(顺序不能反)情况,记录下位置,然后输出pow(2,tag+1)-1就行了

原理是可以把该位后面的数字改成每一位是0 1或者1 0,也就是异或一定为1,

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<string>
  6. #include<stdio.h>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<deque>
  12. #include<algorithm>
  13. #define ll long long
  14. #define INF 1008611111
  15. #define M (t[k].l+t[k].r)/2
  16. #define lson k*2
  17. #define rson k*2+1
  18. using namespace std;
  19. int main()
  20. {
  21. int a[105],b[105],c[105],len1=0,len2=0;
  22. memset(a,0,sizeof(a));
  23. memset(b,0,sizeof(b));
  24. ll l,r,i,j;
  25. scanf("%lld%lld",&l,&r);
  26. if(l==r)
  27. {
  28. printf("0\n");
  29. return 0;
  30. }
  31. while(l!=0)
  32. {
  33. a[len1]=l%2;
  34. l/=2;
  35. len1++;
  36. }
  37. while(r!=0)
  38. {
  39. b[len2]=r%2;
  40. r/=2;
  41. len2++;
  42. }
  43. int tag=0;
  44. for(i=max(len1,len2)-1;i>=0;i--)
  45. {
  46. if(b[i]==1&&a[i]==0)
  47. {
  48. tag=i;
  49. break;
  50. }
  51. }
  52. printf("%lld\n",(ll)pow(2,tag+1)-1);
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读