HDU 5661 Claris and XOR
2016-04-26 20:26
459 查看
题目链接: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。
题意: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 %100000007 int 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; }
相关文章推荐
- 数据库连接池简单设计
- 小笨狼与LLDB的故事
- Android ADB工具
- C++作业4
- java中的反射机制,以及如何通过反射获取一个类的构造方法 ,成员变量,方法,详细。。
- HDU 5179 beautiful number 数位dp
- 算法训练 调和数列问题
- JAVA——多线程举例
- #leetcode#121. Best Time to Buy and Sell Stock
- Efficient Graph-Based Image Segmentation
- 用程序获取本机IP地址,mac地址
- 数据结构之二叉树的先序、中序、后续的求法
- HDU 4151 The Special Number(预处理打表+二分)
- android 之view的测量和绘制(群英传读书笔记1)
- 在linux中添加环境变量
- VS2015如何自定义类模板、我的模板——原来这么简单!
- UVA-1631 Locker(DP)
- 解决Scrapy性能问题——案例四(响应太多导致溢出)
- c++第四次作业
- JAVA基础总结