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. Django:之不得不说的web框架们

    python的web框架 Bottle Bpttle是一个快速.简洁.轻量级的基于WSIG的微型web框架,此框架只有一个.py文件,除了python的标准库外,其不依赖任何其它模块. pip ins ...

  2. Git合并分支出现的冲突解决

    人生不如意之事十有八九,合并分支往往也不是一帆风顺的. 我们准备新的分支newbranch. LV@LV-PC MINGW32 /c/gitskill (master)$ git checkout - ...

  3. 简单Spring和mybatis整合配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  4. 如何修改android工程的包名?

    在我们android项目开发到一定的程度时由于需要,我们必须修改一下工程的包名,以便更好的发布我们的项目.但是在这个过程中有时候修改好了之后会出现一些错误.下面由小编一步步教你如何更改包名,和解决出现 ...

  5. C++设计模式-Singleton单例模式

    template <typename T> class Singleton { public: template <typename... Args> static T* In ...

  6. ubuntu 上下载PHP的源代码

    参考: https://vpsineu.com/blog/how-to-build-and-install-php-5-6-9-from-source-on-ubuntu-14-04-vps/ 直接 ...

  7. java应用测试报告生成(一): sonarqube配合Jenkins生成测试报告及覆盖率

    环境准备: 1.Jenkins集成环境(安装 sonarqube插件) 2.安装sonarqube服务(下载sonarqube安装包并解压,目录到"sonarqube-5.4/bin/lin ...

  8. 观光公交noip<贪心>

    题目链接:https://www.oj.swust.edu.cn/problem/show/1190 思路: 每在一段路上使用一次加速器,就会对某些人或者说某些路段上的人产生影响,目的是使产生的影响最 ...

  9. 网络获取的XML的Pull解析

    <?xml version="1.0" encoding="utf-8" ?> - <students> - <student x ...

  10. 学习笔记——建造者模式Builder

    构造者模式.外部场景如果需要一个汽车类,它不需要关心如何构造,它只需要告诉Director需要什么,就可以从Director获得. 如:CDirector(IBuilder* aBuilder); 场 ...