Codeforces 276D Little Girl and Maximum XOR 贪心+构造
来源:互联网 发布:开淘宝店需要什么资料 编辑:程序博客网 时间:2024/05/24 05:31
点击打开链接
题意: 给定l和r,l,r<=1e18, 求l-r之间的两个数a,b, 使得a^b有最大值.
把l,r展开成2进制000111...
000110...
若l,r二进制中第一个不相同为第k位,则取a第k位之后全部取1,b第k位之后全取0
此时答案的第k位起全部为1,显然为最大.
因为ans第k位前不可能有1,a,b必须在l,r内,若k位之前,l,r某位为(1,1)时,若a,b其中一个该位取0,必然小于l.
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;int main(){ ll a,b; cin>>a>>b; int i=61; for(;i>=0;i--) { if((a&(1LL<<i)) ^ (b&(1LL<<i))) break; } ll ans=0; ans=(1LL<<(i+1))-1; cout<<ans<<endl; return 0;}
解法2(xjb写的)
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=2e3+20;ll l,r,a,b,p[80];int main(){p[0]=1;for(int i=1;i<=64;i++)p[i]=p[i-1]*2;while(cin>>l>>r){a=b=0;ll ans=0;for(int i=64;i>=0;i--){ll x=p[i],y=p[i+1]-1;//从高位贪心构造a和b:该位能否1个1,一个0//a,b分别取1,取0当且仅当两个数要满足:最大要>=l,最小要<=r //感觉还是有点问题的,水过了if(a+x<=r&&(a+y>=l)&&b+x-1>=l&&b+x-1<=r){a+=x;//..1000b+=x-1;//0111ans+=y;break;}//否则该位:只能两个都为1 或者都为0 if(a+y>=l&&a+x<=r&&b+y>=l&&b+x<=r){a+=x;b+=x;}}cout<<ans<<endl;}return 0;}
0 0
- Codeforces 276D Little Girl and Maximum XOR 贪心+构造
- Codeforces 276D Little Girl and Maximum XOR (贪心)
- Codeforces 276D Little Girl and Maximum XOR【贪心】
- D. Little Girl and Maximum XOR(贪心)
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- Codeforces 276D. Little Girl and Maximum XOR
- Codeforces 276D. Little Girl and Maximum XOR(模拟)
- [CodeForces 276D] Little Girl and Maximum XOR (构造+脑洞)
- CodeForces 276 D.Little Girl and Maximum XOR(位运算)
- codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】
- 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 276C Little Girl and Maximum Sum
- codeforces 276 C. Little Girl and Maximum Sum
- Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum(简单贪心)
- N - Little Girl and Maximum Sum——贪心
- CF276C Little Girl and Maximum Sum(贪心+差分)
- 不能声明为虚函数的函数:
- 理解GC日志
- 课堂知识整理
- leetcode解题之107 # Binary Tree Level Order Traversal II Java版
- Java中类加载的时机
- Codeforces 276D Little Girl and Maximum XOR 贪心+构造
- bzoj1066: [SCOI2007]蜥蜴
- 基于Cocopods的模块化管理实现
- 蓝桥杯-凑算式
- linux里面的简单命令
- 权限管理系统
- POJ 2246 Matrix Chain Multiplication 栈
- 1002. 写出这个数 (20)
- 一键google遨游