您的位置:首页 > 其它

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。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: