Claris and XOR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 332    Accepted Submission(s): 135

Problem Description
Claris
loves bitwise operations very much, especially XOR, because it has many
beautiful features. He gets four positive integers a,b,c,d that satisfies a≤b and c≤d. He wants to choose two integers x,y that satisfies a≤x≤b and c≤y≤d, and maximize the value of x XOR y. But he doesn't know how to do it, so please tell him the maximum value of x XOR y.
 
Input
The first line contains an integer T(1≤T≤10,000)——The number of the test cases.
For each test case, the only line contains four integers a,b,c,d(1≤a,b,c,d≤1018). Between each two adjacent integers there is a white space separated.
 
Output
For each test case, the only line contains a integer that is the maximum value of x XOR y.
 
Sample Input
2
1 2 3 4
5 7 13 15
 
Sample Output
6
11

Hint

In the first test case, when and only when $x=2,y=4$, the value of $x~XOR~y$ is the maximum.
In the second test case, when and only when $x=5,y=14$ or $x=6,y=13$, the value of $x~XOR~y$ is the maximum.

 

昨天的B题,我的理解力,我都惭愧了,自己写了好几个样例才完全搞懂。

从最高位到最低位贪心。

贪心时,如果x走到此地方
   x                   y
b 1 1 0 0 0    d  1 0 0 1 1
a 1 0 0 0 0    c   1 0 0 0 1
 
此时x1 = x2, y1 =y2  而且x1 = y1,所以有唯一解,取 0. 如果y系列等于0 有唯一解 1
 
   x                   y
b 1 0 1 0 1 1    d  1 0 0 0 0 0

a    1 0 0 0 0    c   1 0 0 0 0 0

x1 != x2, y1 == y2,贪心使其为1.  如果y = 1,所以x = 0,此时二进制有五位数,设其为C, 所以10000 <= c <= 11111, 所以让b = ((ll)1<<i) - 1. 同理。
 
   x                   y
b 1 0 1 0 1 1    d  1 0 0 0 0 0

a    1 0 0 0 0    c   0 1 0 0 0 0

此时x1 != x2, y1 != y2, 0 1可任意取, 前面取 11111 后面取100000 ,跳出。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
void solve(){
int t;
scanf("%d",&t);
while(t--){
ll a,b,c,d;
cin>>a>>b>>c>>d;
int x1,x2,y1,y2;
ll ans = ;
for(int i = ; i>=; i--){
x1 = (bool)(a&((ll)<<i));
x2 = (bool)(b&((ll)<<i));
y1 = (bool)(c&((ll)<<i));
y2 = (bool)(d&((ll)<<i));
if(x1 == x2&&y1 == y2){
if(x1 != y1){
ans += ((ll)<<i);
}
}
else if(x1 != x2&&y1 == y2){
ans += ((ll)<<i);
if(y1 == ){
b = ((ll)<<i) - ;
}
else{
a = ((ll)<<i);
}
}
else if(x1 == x2&&y1 != y2){
ans += ((ll)<<i);
if(x1 == ){
d = ((ll)<<i) - ;
}
else{
c = ((ll)<<i);
}
}
else if(x1 != x2&&y1 != y2){
ans += ((ll)<<(i+))-;
break;
}
}
cout<<ans<<endl;
}
}
int main()
{
solve();
return ;
}

卷珠帘

 

Claris and XOR(模拟)的更多相关文章

  1. BC之Claris and XOR

    http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...

  2. hdu 5661 Claris and XOR

    Claris and XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. Claris and XOR

    Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...

  4. HDU5661 Claris and XOR

    我们求二进制是怎么求的呢:先看看二进制的每一位代表多大:.......32 16 8 4 2 1 假如n=10, ..... 32>n ,不要. 16>n,不要. 8<=n,要,然后 ...

  5. HDU 5661 Claris and XOR 贪心

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  6. 【整理】XOR:从陌生到头晕

    一:解决XOR常用的方法: 在vjudge上面输入关键词xor,然后按照顺序刷了一些题. 然后大概悟出了一些的的套路: 常用的有贪心,主要是利用二进制的一些性质,即贪心最大值的尽量高位取1. 然后有前 ...

  7. HDU 5683 zxa and xor 暴力模拟

    zxa and xor 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5683 Description zxa had a great interes ...

  8. 清北学堂模拟赛d1t6 或和异或(xor)

    题目描述 LYK最近在研究位运算,它研究的主要有两个:or和xor.(C语言中对于|和^) 为了更好的了解这两个运算符,LYK找来了一个2^n长度的数组.它第一次先对所有相邻两个数执行or操作,得到一 ...

  9. HDU 4825 Xor Sum(二进制的字典树,数组模拟)

    题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #in ...

随机推荐

  1. c++ lower_bound upper_bound

    lower_bound, first greater than or equal to upper_bound, first strickly greater

  2. JS调用OC方法

    - (void)myMethod:(CDVInvokedUrlCommand*)command { NSString* echo = [command.arguments objectAtIndex: ...

  3. spring securiy使用总结

    我们常见的几个功能: 注册后直接登录,并且remember-me这种在网上找到很多注册后登录的,但是remember-me没有.其实解决方案还是看源码比较方便.a. 装载authenticationM ...

  4. go mode

    https://github.com/dominikh/go-mode.el http://blog.altoros.com/golang-part-1-main-concepts-and-proje ...

  5. JQuery获取input type="text"中的值的各种方式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 转 当当网资深DBA:DB运维四大现代化的实现

    位好,今天我的主题是 <DB运维的四个现代化> ,看标题就能明白,是关于DBA自动化运维平台的事情.http://dbaplus.cn/news-21-855-1.html 主要是分享下我 ...

  7. Servlet与jsp间的传值问题

    Servlet与JSP 之间的传值有两种情况:JSP -> Servlet, Servlet -> JSP.通过对象 request和 session (不考虑 application)完 ...

  8. C语言中static作用

    在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)先来介绍它的第一条也是最重要的一条:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有 ...

  9. java 常见异常总结

    异常1:java.util.NoSuchElementException: No line found 原因:Java 是顺序执行的 你执行到.close() 后就代表 你关闭了 流,你再去调用已经被 ...

  10. HTTP代理浅说

    简单的说HTTP代理就是处于HTTP客户端和服务器端之间,中转消息的中间人. 一种代理是代客户端去请求服务器,叫做Forward Proxy正向代理:另一种是代理真正的服务器来接收用户请求,叫做Rev ...