codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】
来源:互联网 发布:事业单位会计软件 编辑:程序博客网 时间:2024/06/01 16:50
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 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.
In a single line print a single integer — the maximum value of for all pairs of integers a, b (l ≤ a ≤ b ≤ r).
1 2
3
8 16
31
1 1
0
【题解】
刚开始一看到数据范围惊呆了,但是最后仔细思考以后发现是有技巧可言的,首先,异或值要为1,那么两个数的对应2位进制位要不同,而异或值要最大,则二进制的高位要尽可能的为1,所以这就是切入点,从给定的区间上下限入手(l和R),从l和r二进制的最高位开始比较,如果出现对应位异或值位1,就从该处开始,低位都置为1,此时其表示的数就是最大异或值了。
【AC代码】
#include<cstdio>#include<algorithm>#include<cstdio>#include<math.h>#include<string.h>#include<iostream>using namespace std;typedef __int64 ll;ll l,r;int main(){ while(~scanf("%I64d%I64d",&l,&r)) { ll max_xor = 0; int i = 61; for(;i>=0;--i) { if(l&(1LL<<i) ^ r&(1LL<<i)) break; } for(;i>=0;--i) { max_xor|= 1LL<<i; } printf("%I64d\n",max_xor); } return 0;}
- codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】
- Codeforces 276D. Little Girl and Maximum XOR
- Codeforces 276D Little Girl and Maximum XOR (贪心)
- Codeforces 276D Little Girl and Maximum XOR 贪心+构造
- Codeforces 276D Little Girl and Maximum XOR【贪心】
- Codeforces 276D. Little Girl and Maximum XOR(模拟)
- 【Codeforces Round 169 (Div 2) D】【简单数位贪心】Little Girl and Maximum XOR 区间选两数使得异或值尽可能大
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- D. Little Girl and Maximum XOR(贪心)
- [CodeForces 276D] Little Girl and Maximum XOR (构造+脑洞)
- CodeForces 276 D.Little Girl and Maximum XOR(位运算)
- Codeforces 276C Little Girl and Maximum Sum 线段树区间累加
- Codeforces 276C Little Girl and Maximum Sum(线段树的区间更新)
- CodeForces 276C Little Girl and Maximum Sum
- Codeforces 276C Little Girl and Maximum Sum
- codeforces 276 C. Little Girl and Maximum Sum
- Codeforces 276C Little Girl and Maximum Sum【贪心】
- Codeforces 246C Little Girl And Maximum 差分
- PHP 中 16 个魔术方法详解
- mac平台编译sdl2.0.6和调试debug sdl
- 浅水声信道模型的建立
- 2017.10.22_数位dp模板分析
- Oracle 系统权限、对象权限、角色权限
- codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】
- 坑题
- php ajax 点击'查看更多'小功能
- 51nod 1396 还是01串 【思维题】
- 关于二进制位移“>>”和“>>>”的区别小结
- 作业3
- C#基础(3)——异常捕获与断点调试
- 分布式ID生成器
- nodejs异常之-Error: listen EACCES 127.0.0.1:8000