BC之Claris and XOR
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
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.
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.
2
1 2 3 4
5 7 13 15
6
11HintIn 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.
我真也是服了,一个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的更多相关文章
- hdu 5661 Claris and XOR
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Claris and XOR(模拟)
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Claris and XOR
Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...
- HDU5661 Claris and XOR
我们求二进制是怎么求的呢:先看看二进制的每一位代表多大:.......32 16 8 4 2 1 假如n=10, ..... 32>n ,不要. 16>n,不要. 8<=n,要,然后 ...
- HDU 5661 Claris and XOR 贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- 【整理】XOR:从陌生到头晕
一:解决XOR常用的方法: 在vjudge上面输入关键词xor,然后按照顺序刷了一些题. 然后大概悟出了一些的的套路: 常用的有贪心,主要是利用二进制的一些性质,即贪心最大值的尽量高位取1. 然后有前 ...
- 【机器学习】神经网络实现异或(XOR)
注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...
- 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 ...
- XOR Queries(莫队+trie)
题目链接: XOR Queries 给出一个长度为nn的数组CC,回答mm个形式为(L, R, A, B)(L,R,A,B)的询问,含义为存在多少个不同的数组下标k \in [L, R]k∈[L,R] ...
随机推荐
- Node.js高效按行输出文件内容
const fs = require('fs'); const EventEmitter = require('events'); const util = require('util'); cons ...
- Easyui扩展icon下载
链接:http://pan.baidu.com/s/1eS7bh0e 密码:owzp 来源:https://github.com/cjw0511/jquery-extensions
- python 中md5 和 sha1 加密, md5 + os.urandom 生成全局唯一ID
首先先来介绍一下md5 和 sha1 的概念 MD5 MD5的全称是Message-Digest Algorithm 5(信息-摘要算法).128位长度.目前MD5是一种不可逆算法. 具有很高的安全性 ...
- java虚拟机之引用
强引用: 类似:object A=new Object();这样的引用,只要强引用还存在,垃圾回收期就永远不会回收被引用的对象,eg:这里的new Oject(). 软引用: 一些还有用,但是非必 ...
- ion-refresher 下拉更新数据
使用指令ion-refresher可以为容器eg:ion-scroll 和 ion-content进行拉动刷新 <ion-scroll> <ion-refresher on-refr ...
- ion-header-bar
ion-header-bar 指令声明一个标题栏元素,标题栏总是位于屏幕顶部 它有两个同级的可选属性 align-title:设置标题文字的对齐方式.允许值:left|right|center no- ...
- Java实验三
20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...
- CSS样式--实际开发总结
1. div 嵌套,子div中内容超出范围可以设置: display:inline-block; overflow:auto 即可让子div中出现滚轴 2. 让div中内容垂直方向居中 设置: ...
- Longest Increasing Subsequence
很久不写算法了== 写个东西练练手 最长上升子序列 输入n,然后是数组a[ ]的n个元素 输出最长上升子序列的长度 一.最简单的方法复杂度O(n * n) DP[ i ] 是以a[ i ] 为结尾的最 ...
- SVN-Server搭建及配置
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...