CodeForces 276 D.Little Girl and Maximum XOR(位运算)
来源:互联网 发布:mmd 动作数据 r-18 编辑:程序博客网 时间:2024/06/07 06:25
Description
从区间[L,R]中找两个数x,y使得x^y值最大
Input
两个整数L和R(1<=L<=R<=1e18)
Output
输出x^y的最大值
Sample Input
1 2
Sample Output
3
Solution
考虑L和R的二进制表示,前面相同的位跳过,因为x和y无论怎么取这些位都是固定的,那么对x^y就没有贡献,从L和R的第一个不同位开始,如果R是1则令y对应位是1,x对应位是0,如果R是0则令y对应位是0,x对应位是1(一定可以取到的原因是L<=R,即使x这位取1也一定在[L,R]中,因为最高一个不同位R是1而L是0),那么这样一直取下去可以使得x和y的每一位都不同,那么异或的结果就是每一位都是1,故直接看L^R的最高位数x即可,答案就是2^x-1
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 1111int main(){ ll l,r; while(~scanf("%I64d%I64d",&l,&r)) { ll ans=0; l^=r; while(l) { ans=ans*2ll+1; l>>=1; } printf("%I64d\n",ans); } return 0;}
0 0
- CodeForces 276 D.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 276D] 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 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 Round 169 (Div 2) D】【简单数位贪心】Little Girl and Maximum XOR 区间选两数使得异或值尽可能大
- CodeForces - 276C Little Girl and Maximum Sum (扫描线)
- Codeforces 246C Little Girl And Maximum 差分
- Little Girl and Maximum Sum
- 模型评估与选择-----第二部分
- 下沉的船 oj
- Python运维之获取操作系统的内存信息
- 初识sed用法
- 哈希算法---Equations
- CodeForces 276 D.Little Girl and Maximum XOR(位运算)
- yes 这是~4月份学习计划
- 实例浅析IO阻塞、非阻塞epoll的水平触发和边缘触发
- 学习Linux命令(1)
- windows下安装redis 以及phpredis的扩展 (windows redis php&php7)
- POJ 3311 状压+Floyd
- Python运维之获取系统CPU信息
- 鸟哥的linux私房菜学习笔记2
- Linux Kernel 学习笔记1:module的基本结构和编译