http://acm.hdu.edu.cn/showproblem.php?pid=5661

Claris and XOR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 146    Accepted Submission(s): 51

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.
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5664 5663 5662 5661 5660 
 

我真也是服了,一个1<<50一样的数据彻底把我打败,从BC的时候,到赛后自己狂敲,然后狂不对,改dfs,超时,依旧不对,我去真也是醉了,看题解思路稍有差别,剪枝,依旧WR

各种测试,我去都不对,然后忽然发现有个1<<num的,应该越界了,改之,不到100毫秒就A掉,直接无语,又被细节打败了。

官方题解:

考虑从高位到低位贪心,对于每一位,如果x,y只有唯一的取法,那么只能这么取;否则贪心地必须使答案的这一位等于1。如果x,y都是0,1都能取,则设这是从右向左数第len位,因为x,y能取的值一定都是连续的一段,因此x,y的后len位都能取0111...1(len-1个1)和1000...0(len-1个0)(否则做不到从右向左数第len位都能取0,1)。也就是说,后len位的贡献一定能达到可能的上界111...1(len个1)。此时不必继续考虑后面的位。

如果x,y在这一位并不是0,1都能取,那么由于要使得答案的这一位等于1,也只有唯一的取法。

至此,这一位考虑完毕,然后根据选取的方案,修正一下x和y的范围,然后对后一位做即可。

细节决定成败。。。

下面是几种思路,既然想了就都粘上吧,都能过。(不多转2进制的函数没用,直接1<<判断就好了。。。)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define MAX 100005
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<string,int>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
using namespace std;
char ch[100];
string getbia(LL num){
int i=0;
string str="",str1;
while(num){
if(num%2) str+="1";
else str+="0";
num=num>>1;
// cout<<"OOOOOOO"<<endl;
}
int len = str.length();
for(int i=len-1;i>=0;i--)
str1+= str[i];
return str1;
}
int main()
{
int T;
LL a ,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&a,&b);
scanf("%I64d%I64d",&c,&d);
if(b>d){
swap(a,c);
swap(b,d);
}
string str1=getbia(d);
string str2=getbia(b);
//cout<<str1<<endl;
int maxLen = str1.length();
LL res=0;
for(int i=0;i<=maxLen;i++){
//cout<<a<<' ' <<b<<' '<<c<<' '<<d<<endl;
LL temp=(LL)1<<(maxLen-1-i);
int mark=0;
if(temp<=d){ ///可以是1
if(temp-1>=a){
res+=temp,mark=1;
d = d>=temp ? d-temp : d;
c = c>=temp ? c-temp : 0;
a = a>=temp ? temp-1 : a;
b = b>=temp ? temp-1 : b;
}
}
if(temp-1>=c&&!mark) { ///可以是0
if(temp<=b){
res+=temp,mark;
d = d>=temp ? temp-1 : d;
c = c;
a = a>=temp ? a-temp : a;
b = b>=temp ? b-temp : 0;
}
}
if(!mark){
d = d>=temp ? d-temp : d;
c = c>=temp ? c-temp : c;
a = a>=temp ? a-temp : a;
b = b>=temp ? b-temp : b;
}
if(a>b) swap(a,b);
if(c>d) swap(c,d);
if(b>d){
swap(a,c);
swap(b,d);
}
}
printf("%I64d\n",res);
}
return 0;
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define MAX 100005
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<string,int>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
using namespace std;
char ch[100];
string getbia(LL num){
int i=0;
string str="",str1;
while(num){
if(num%2) str+="1";
else str+="0";
num=num>>1;
// cout<<"OOOOOOO"<<endl;
}
int len = str.length();
for(int i=len-1;i>=0;i--)
str1+= str[i];
return str1;
}
//LL tempRes=0;
LL dfs(LL a,LL b,LL c,LL d,int maxLen,LL res){
LL temp=(LL)1<<(maxLen);
if(maxLen<0) return res;
LL a1,b1,c1,d1;
LL res1=0,res2=0,res3=0,mark=0; if((temp<=d&&temp-1>=c) &&(temp-1>=a&&temp<=b)){ ///开始没有这段优化,一直是超时的。。。
return res+((LL)1<<(maxLen+1))-1;
}
if(temp<=d){ ///可以是1
if(temp-1>=a){
mark=1;
d1 = d-temp ;
c1 = c>=temp ? c-temp : 0;
a1 = a;
b1 = b>=temp ? temp-1 : b;
res1 = dfs(a1,b1,c1,d1,maxLen-1,res+temp);
}
}
if(temp-1>=c&&!mark) { ///可以是0
if(temp<=b){
mark=1;
d1 = d>=temp ? temp-1 : d;
c1 = c;
a1 = a>=temp ? a-temp : 0;
b1 = b-temp;
res2 = dfs(a1,b1,c1,d1,maxLen-1,res+temp);
}
}
if(!mark){ ///此位异或后不可能为1
// cout<<"4:";
a1 = a>=temp ? a-temp : a;
b1 = b>=temp ? b-temp : b;
d1 = d>=temp ? d-temp : d;
c1 = c>=temp ? c-temp : c;
res3 = dfs(a1,b1,c1,d1,maxLen-1,res);
}
LL mmax = max(res1,max(res2,res3)) ;
return mmax;
} int main()
{
int T;
LL a ,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&a,&b);
scanf("%I64d%I64d",&c,&d);
if(b>d){
swap(a,c);
swap(b,d);
}
string str1=getbia(d);
string str2=getbia(b);
int maxLen = str1.length();
LL res=dfs(a,b,c,d,maxLen-1,0);
printf("%I64d\n",res);
}
return 0;
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define MAX 100005
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<string,int>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
using namespace std;
char ch[100];
string getbia(LL num){
int i=0;
string str="",str1;
while(num){
if(num%2) str+="1";
else str+="0";
num=num>>1;
// cout<<"OOOOOOO"<<endl;
}
int len = str.length();
for(int i=len-1;i>=0;i--)
str1+= str[i];
return str1;
}
int main()
{
int T;
LL a ,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&a,&b);
scanf("%I64d%I64d",&c,&d);
if(b>d){
swap(a,c);
swap(b,d);
}
string str1=getbia(d);
string str2=getbia(b);
//cout<<str1<<endl;
int maxLen = str1.length();
LL res=0;
for(int i=0;i<=maxLen;i++){
//cout<<a<<' ' <<b<<' '<<c<<' '<<d<<endl;
LL temp=(LL)1<<(maxLen-1-i);
int mark=0;
if(temp<=d){ ///可以是1
if(temp-1>=a){
res+=temp,mark=1;
d = d>=temp ? d-temp : d;
c = c>=temp ? c-temp : 0;
a = a>=temp ? temp-1 : a;
b = b>=temp ? temp-1 : b;
}
}
if(temp-1>=c&&!mark) { ///可以是0
if(temp<=b){
res+=temp,mark;
d = d>=temp ? temp-1 : d;
c = c;
a = a>=temp ? a-temp : a;
b = b>=temp ? b-temp : 0;
}
}
if(!mark){
d = d>=temp ? d-temp : d;
c = c>=temp ? c-temp : c;
a = a>=temp ? a-temp : a;
b = b>=temp ? b-temp : b;
}
if(a>b) swap(a,b);
if(c>d) swap(c,d);
if(b>d){
swap(a,c);
swap(b,d);
}
}
printf("%I64d\n",res);
}
return 0;
}

BC之Claris and XOR的更多相关文章

  1. hdu 5661 Claris and XOR

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

  2. 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. 【机器学习】神经网络实现异或(XOR)

    注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...

  8. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  9. XOR Queries(莫队+trie)

    题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...

随机推荐

  1. js获取url的常用方法

    //设置或获取对象指定的文件名或路径. console.log(window.location.pathname) //设置或获取整个 URL 为字符串. console.log(window.loc ...

  2. http 学习 1-1 chapter1-HTTP概述

    Web浏览器.服务器和相关的Web应用程序都是通过HTTP相互通信的.HTTP是现代全球因特网中使用的公共语言. 1.1HTTP – 因特网的多媒体信使 HTTP使用的是可靠的数据传输协议,确保数据在 ...

  3. dedecms 后台发布后的文章不能编辑出现一片空白的解决办法

    dede后台无法写新文章也无法编辑以前的文档,文章编辑的地方无法写入,出现无法显示该页面的问题,这是怎么回事?我的dede在ftp中换过文件夹,是不是跟这个有关?该如何解决这个问题? 以下修改是针对d ...

  4. CoreData数据库

        一  CoreData 了解 1 CoreData 数据持久化框架是 Cocoa API 的一部分,首先在iOSS5 版本的系统中出现:      它允许按照 实体-属性-值 模式组织数据: ...

  5. toUnsignedString详解

    /** * All possible chars for representing a number as a String */ final static char[] digits = { '0' ...

  6. php面试题及答案

    1.用PHP打印出前一天的时间,格式是2006-5-10 22:21:21 <?php   //echo date('Y-m-d H:i:s',time()-60*60*24   echo da ...

  7. 计算机启动boot

    原创博文:转载请标明出处:http://www.cnblogs.com/zxouxuewei 零.boot的含义 先问一个问题,"启动"用英语怎么说? 回答是boot.可是,boo ...

  8. code complete part1

    最近在看code complete,学习了一些东西,作为点滴,记录下来. 关于类: 类的接口抽象应该一致 类的接口要可编程,不要对类的使用者做过多的假设.不要出现类似于:A的输入量一定要大于多少小于多 ...

  9. 用js判断页面刷新或关闭的方法

    Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定.区别在于on ...

  10. VC++常用函数

    // 获取文件运行路径 void GetAppFilePath(const CString& strProjectName) { TCHAR szFileName[MAX_PATH]; HMO ...