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] ...
随机推荐
- ARP 扫描主机学习笔记
1.通用套接字地址结构与具体套接字地址结构之间可相互转化 1)通用转具体,某些函数将结果存储在通用套接字地址结构中,这时将通用转换为具体,具体通过访问成员名可以方便的得到结果. 2)具体转通用,为了消 ...
- 算法小节(一)——斐波那契数列(java实现)
看到公司的笔试题中有一道题让写斐波那契数列,自己忙里偷闲写了一下 什么是斐波那契数列:斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- 配置不当导致无法加载odoo-10.0模块
启动odoo-bin时出错 2017-01-05 06:38:51,046 5480 INFO ? odoo: Odoo version 10.02017-01-05 06:38:51,046 548 ...
- CE 内存申请
char ch_ReadByte='H'; char *ptr_OneLineData; unsigned ); if ((ptr_OneLineData = (char *)malloc(bufsi ...
- Quartz2D之生成圆形头像、打水印、截图三种方法的封装
我给UIImage类添加了一个类目,用于封装三个方法,每个方法都没有难度,做这个主要为了练习一下封装: 首先在类目.h文件中声明三个方法:以及创建了一个枚举.用于水印方法中设定水印位置:方法说明和参数 ...
- Chapter 3: Develop the user experience
Plan for search engine optimization and accessibility 使用analytical tools分析HTML,如SEO toolkit from MS, ...
- zend studio 配置 apache服务器事宜
安装好 zend studio后,配置 apache服务器时,设置 configuration directory时,需选中 xampp\apache里面的 conf 文件夹,即完整的路径为: *\x ...
- RPC与hadoop
rlgdj的这样的话,真正的实现类在Server端,客户端调用方法的时候,只能得到得到从Server端的返回值.看来接口中的抽象方法必须要有返回值啊.ps.右下角的Client端的main()中rpc ...
- jquery属性的操作
HTML示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- [转]PHP如何关闭notice级别的错误提示
1.在php.ini文件中改动error_reporting改为: error_reporting=E_ALL & ~E_NOTICE 2.如果你不能操作php.ini文件,你可以使用如下方法 ...