HDU 5661 Claris and XOR
来源:互联网 发布:淘宝店铺过户条件 编辑:程序博客网 时间:2024/06/02 04:49
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5661
题意:x∈[a,b],y∈[c,d],使得x xor y 的值最大,求最大值是多少。
思路:从高位到低位贪心,优先让高位异或值为1,这样得到的数一定是最大的。对于第i位,先看一看x和y是不是能取相异的,如果能取相异的,答案就加上那一位的权值。
判断第i位是否可以取0,如果第i位取0,那么这个数最大就是tx+bit[i-1]+bit[i-2]+...1(tx为高于i位的数位取1产生的值,bit[i]表示第i位的权值),如果这个最大数能达到范围下界就合法。同理判断第i位是否可以取1,看第i位取1时达到的最小数是否不超过范围上界。tx+bit[i]<=lim。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod %100000007int T;LL a,b,c,d;LL ans;LL bit[70];bool zero(int i , LL lim , LL now){ return now-1>=lim-bit[i];}bool one(int i , LL lim , LL now){ return now<=lim-bit[i];}int main(){ bit[1] = 1; rep(i,2,63) bit[i] = bit[i-1]<<1; cin>>T; while(T--) { scanf("%I64d %I64d %I64d %I64d",&a,&b,&c,&d); ans = 0; ULL tx,ty; tx = ty = 0; bool x_one,x_zero; bool y_one,y_zero; Rrep(i,61,1) { x_one = one(i,b,tx); x_zero = zero(i,a,tx); y_one = one(i,d,ty); y_zero = zero(i,c,ty); if ( (x_one && y_zero) || ( x_zero && y_one ) ) //可以相异 { ans+=bit[i]; if ( x_one && y_zero ) tx+=bit[i]; //第一个数的第i位取1 else ty+=bit[i];//第二个数的第i位取1 } else { if ( x_one && y_one )//都只能取1 { tx+=bit[i]; ty+=bit[i]; } } } cout<<ans<<endl; } return 0;}
0 0
- HDU 5661 Claris and XOR
- HDU-5661-Claris and XOR
- HDU 5661 Claris and XOR
- Hdu 5661 Claris and XOR
- HDU 5661 Claris and XOR
- HDU-5661 Claris and XOR (贪心)
- HDU 5661 Claris and XOR(贪心)
- 【Hdu 5661】 Claris and XOR 位运算+贪心
- BestCoder Round #79 1002/hdu 5661 Claris and XOR 贪心
- HDU 5661/BC 79B Claris and XOR
- HDU 5661 Claris and XOR(按位异或贪心)
- HDU 5661 Claris and XOR(异或,贪心)
- HDU5661---Claris and XOR
- Claris and XOR
- bestcoder 79 Claris and XOR
- BC之Claris and XOR
- BestCoder Round #79 (div.1) Claris and XOR 题解。。。
- BestCoder Round #79 (div.2) 1002 Claris and XOR
- CSU 1542 Flipping Parentheses
- 数据结构之表
- c++第四次作业
- Android内存优化之OOM
- 输入前序/后序+中序 遍历结果重建二叉树(递归)
- HDU 5661 Claris and XOR
- python:有序字典与嵌套字典
- 使用Fresco下载图片
- OpenGL ES3.0 的初探
- 12个顶级开发者社区网站帮助程序员走上人生巅峰
- hdu 5647 DZY Loves Connecting (树形dp)
- Filter-统一全站编码
- 图(graph)
- 欢迎使用CSDN-markdown编辑器