You have two integers L and R, and you are required to find the max xor value of a and b where L <= a <= R and L <= b <= R

Input

Two integers in a line. L, R <= 1e9

Output

One integer, the answer

Example

Input:
1 10 Output:
15

题意:

给定L,R,X1^X2^X3...最大异或,(L<=X1,X2,X3...<=R)。

没什么思路,上次CF就遇到这道题,我是用贪心写的,忽略pow的精度问题,可以AC。

http://codeforces.com/contest/912/problem/B

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
long long a,b,n,k,ans,c,d;
map<long long,int>mp;
vector<long long>S;
int main()
{
while(~scanf("%I64d%I64d",&n,&k)){
ans=;
for(long long i=log2(n);i>=;i--){
long long tmp=pow(,i);
if(k>){
k--;
mp[tmp]=;
S.push_back(tmp);
ans+=tmp;
}
else{
int L=S.size();
for(int j=;j<L;j++){
if(mp[S[j]]==&&S[j]+tmp<=n&&mp[S[j]+tmp]==) {
mp[S[j]]=;
mp[S[j]+tmp]=;
ans+=tmp;
S.push_back(S[j]+tmp);
break;
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}

但是仔细一想的话,得到了最大了2^n<=R,如果还可以异或一个,那么选择2^n-1就好了。(2^n)xor(2^n-1) =2^(n+1)-1。一定是最大的。

比如2^=10000, n=4,10000 xor 01111 = 11111;不可能还有不这个大的了,毕竟n=4是上界。当然只能选一个的时候,就选本身就好了。当然,为了避免卡精度问题(比如CF就hack我了),pow函数最好比较一下,这里太懒,算了。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int a,b,ans,L,R;
while(~scanf("%d%d",&L,&R)){
if(L==R) printf("%d\n",L);
else {
int a=log2(R);
a=q_pow(,a);
printf("%d\n",a+a-);
}
} return ;
}

---------------------------------------分界线---------------------------------------

妈蛋,上诉解法有误。。。。如果我选的数小于L呢?

所以不行的。比如L=10,R=15,标准答案是7。而我的答案是15。。。CF没有L限定,所以可以过。这个题不一样。

对比这里就知道了 https://vjudge.net/problem/HackerRank-maximizing-xor 还好发现了

SPOJ Find the max XOR value(二进制,贪心即可)的更多相关文章

  1. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  2. [USACO]6.1.3 cow xor(二进制+Trie)

    题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...

  3. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  4. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- XOR(二进制使用)

    链接:https://www.nowcoder.com/acm/contest/116/H来源:牛客网 题目描述 Once there was a king called XOR, he had a ...

  5. Codeforces Round #649 (Div. 2) E. X-OR 交互 二进制 随机 期望

    LINK:X-OR 本来是应该昨天晚上发的 可是昨天晚上 做这道题 写了一个分治做法 一直wa 然后查错 查不出来 心态崩了 想写对拍 发现交互库自己写不出来. 一系列sb操作 == 我都醉了. 今天 ...

  6. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  7. [NOI2014]起床困难综合症(二进制+贪心)

    题目 [NOI2014]起床困难综合症 做法 先用全\(0\)和全\(1\)去运行一下,再在满足\(m\)的限制下,贪心地从高位到低位选择即可

  8. Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)

    E. XOR Guessing time limit per test1 second memory limit per test256 megabytes inputstandard input o ...

  9. [WC2011]最大XOR和路径(贪心+线性基)

    题目大意:给一张无向图,求一条1-n的路径,是路径边权的异或和最小. 题解 这道题的思路很妙,首先我们可以随便找出一条从1到n的路径来,然后我们可以选一些环. 其实不管这个环和这条路径有怎样的关系,我 ...

随机推荐

  1. mycat安装和测试

    mycat安装和测试 一.  环境准备 本机环境是三台centos6.5 IP 主机名 数据库名 安装软件 192.168.17.4 master db1 mycat,mysql 192.168.17 ...

  2. 机器学习(十三)——机器学习中的矩阵方法(3)病态矩阵、协同过滤的ALS算法(1)

    http://antkillerfarm.github.io/ 向量的范数(续) 范数可用符号∥x∥λ表示. 经常使用的有: ∥x∥1=|x1|+⋯+|xn| ∥x∥2=x21+⋯+x2n−−−−−− ...

  3. C#中异常处理和Java的区别

    捕获异常,同样是try...catch...,这个完全一样: 抛出异常,同样是throw,这个完全一样: 函数抛出怎样的异常,Java中可以用throws定义,而C#中不用定义,相当于throws E ...

  4. loarocks install loadcaffe 失败

    loarocks install loadcaffe 失败 1.Error: Your user does not have write permissions in /home/zhangliang ...

  5. javascript之Ajax获取和设置标头

    XMLHttpRequest对象中与标头有关的方法: setRequestHeader(<header>,<value>)--用指定值设置标头. getResponseHead ...

  6. cocos2d-x入口类

    上一篇文章中有一个在栈中创建的实例--AppDelegate.这个类的初始化使cocos2d-x的程序能够执行起来.由于它是继承于CCApplication类.而执行的run方法就是在此类中实现的. ...

  7. servelet

    获取页面传递过来的参数. 调用后台代码实现相关业务逻辑. 根据返回结果,进行页面跳转.   问题:httpsession? 拆解: String 里面的.spllit方法.拆开后,返回值为string ...

  8. (利用DOM)在新打开的页面点击关闭当前浏览器窗口

    1.在开发过程中我们前端的用户体验中有时候会要求点击一个按钮,关闭当前浏览器窗口,用HTML DOM就可做到 2.注意:本次写法要求在新窗口中关闭.target="_blank" ...

  9. ffmpeg H264 编解码配置

    ffmpeg H264编解码前面有文章介绍下,本文主要介绍一些参数配置. 编码: int InitEncoderCodec( int iWidth, int iHeight) { AVCodec * ...

  10. 最简单的基于FFmpeg的移动端样例附件:SDL Android HelloWorld

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...