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. mvc UrlHelper

    何谓Helper,其实就是在View中为了实现一些灵活功能而写的方法组. 其实ASP.NET MVC的View是Aspx的页面,本身可以声明定义方法,那为什么要有Helper呢? 其实无非是将界面与逻 ...

  2. Entity Framework技巧系列之十 - Tip 37 - 41

    提示37. 怎样进行按条件包含(Conditional Include) 问题 几天前有人在StackOverflow上询问怎样进行按条件包含. 他们打算查询一些实体(比方说Movies),并且希望预 ...

  3. C# WebBrowser函数互相调用

    在使用C#开发winform程序过程中,我们经常会碰到嵌入了一个WebBrowser的浏览器控件.很多时候,我们需要在程序里控制网页的显示方式,或者调用网页当中的某个JS函数,反过来,也有可能网页也需 ...

  4. nefu 943 黑屏

    Description Veda 在用宽高比为a:b的显示器看一部宽高比为c:d的电影.在使用全屏模式看电影时,如果这个比例不相同,那么在显示器上就会出现了一些没有画面的地方,我们暂且称之为“黑屏”( ...

  5. 兼容IE低版本

    1,IE6PNG透明的bug,只需要把png图另存为无杂边的png-8格式 2,在IE6用overflow:hidden清除浮动,要加上zoom:1 3,IE6下盒子的最小高度为20px 如果要小于2 ...

  6. FZU 2113 BCD Code 数位dp

    数位dp,但是很奇怪的是我在虚拟oj上用GUC C++提交会wa,用Visual c++提交正确,但是加上注释后提交又莫名CE--好任性啊 0 ,0 题目思路:看代码吧 注释很详细 #include& ...

  7. - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析

    转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...

  8. 用for while 成绩的有效输入

    #include "stdio.h" void main() { int score,s; printf("请输入你的成绩:"); scanf("%d ...

  9. linux下环境变量PS1设置

    PS1变量中提示符各项含义:   \d :代表日期,格式为weekday month date,例如:Wed Dec 12 \H :完整的主机名称.例如:hostname是debian.linux \ ...

  10. Boyer Moore算法(字符串匹配)

    上一篇文章,我介绍了KMP算法. 但是,它并不是效率最高的算法,实际采用并不多.各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Mo ...