整整10个月后第二次搞这个问题才搞懂........第一次还是太随意了。

解题思路:

经过打表可得规律答案要么是0 要么是2的N次 - 1

要得到最大的XOR值,其值一定是2的N次 - 1

即在 l 和 r 的二进制中,从左到右遍历过去,如果碰到 (2 ^ i) & l 为 1 , (2 ^ i) & r 为 0

即在 l 和 r 之间一定存在 形如 10+ 和01+这样的数。

则可说明在[l , r]中存在 1000000000 和 0111111111 可得到最大XOR值为2的N次 - 1

PS:不会存在首先出现 l 为 0 r 为 1 的情况,因为 l < r

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max (a,b) (((a) > (b)) ? (a) : (b))
#define Min (a,b) (((a) < (b)) ? (a) : (b))
#define Abs (x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template <class T> inline void checkmin (T &a,T b) { if (a > b) a = b; }
template <class T> inline void checkmax (T &a,T b) { if (a < b) a = b; } const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int main(){
int i, j, k, t, n, m, numCase = ;
ll l, r;
while (cin >> l >> r){
for (i = ; i >= ; --i){
if ((l & (1LL << i)) ^ (r & (1LL << i))) break;
}
ll ans = pow (, i + ) - ;
cout << ans << endl;
} return ;
}

题目描述

A little girl loves problems on bitwise operations very much. Here's one of them.

You are given two integers l and r. Let's consider the values of  for all pairs of integers a and b (l ≤ a ≤ b ≤ r). Your task is to find the maximum value among all considered ones.

Expression  means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as "^", in Pascal — as «xor».

输入

The input consists of multiple test cases.

Each test case contains space-separated integers l and r (1 ≤ l ≤ r ≤ 1018).

输出

In a single line print a single integer — the maximum value of  for all pairs of integers ab (l ≤ a ≤ b ≤ r).

---恢复内容结束---

题目描述

A little girl loves problems on bitwise operations very much. Here's one of them.

You are given two integers l and r. Let's consider the values of  for all pairs of integers a and b (l ≤ a ≤ b ≤ r). Your task is to find the maximum value among all considered ones.

Expression  means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as "^", in Pascal — as «xor».

输入

The input consists of multiple test cases.

Each test case contains space-separated integers l and r (1 ≤ l ≤ r ≤ 1018).

输出

In a single line print a single integer — the maximum value of  for all pairs of integers ab (l ≤ a ≤ b ≤ r).

CodeForces 276D – Little Girl and Maximum XOR 贪心的更多相关文章

  1. Codeforces 276D Little Girl and Maximum XOR

    题意:给范围l,r选两个数亦或最大是多少. 思路:找到第一个l和r二进制下不相同的位置i,然后答案就是2^(i+1)-1,因为一个取0一个取1之后,后面的位置全部选1和全部选0,就是这样:011111 ...

  2. LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71

    421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...

  3. Maximum Xor Secondary CodeForces - 281D (单调栈)

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...

  4. CodeForces 280B Maximum Xor Se

    题目链接:http://codeforces.com/contest/280/problem/B 题目大意: 给定一个由n个数组成的一个序列,s[l..r] (1 ≤ l < r ≤ n)代表原 ...

  5. Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用

    http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...

  6. CodeForces 702 A Maximum Increase (贪心,高效算法)

    题意:给定 n 个数,问你连续的最长的序列是几个. 析:从头扫一遍即可. 代码如下: #include <cstdio> #include <string> #include ...

  7. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  8. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  9. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

随机推荐

  1. javamail发送二进制流附件的问题

    最近做个邮件发送功能,要内嵌图片并有附件. 需求很奇怪,图片和附件文件是放在ftp服务器上的,查了下javamail的文档. 添加附件方法如下 MimeBodyPart messageBodyPart ...

  2. 帝国cms7.2自定义列表建立tag效果 代码 教程

    统计记录:(如:select count(*) as total from phome_ecms_news where classid=1 and checked=1) 注:这句SQL的意思是查找统计 ...

  3. Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)

    最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...

  4. Spring Boot Admin Reference Guide

    1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...

  5. python的正则表达式 re

    python的正则表达式 re 本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以是Unicode字符,这点不用担心,python会处理地和Ascii字符一 ...

  6. 使用回调接口实现ActiveX控件和它的容器程序的通讯

    本文阅读基础:有一定的C++基础知识(了解继承.回调函数),对MFC的消息机制有一定了解,对COM的基础知识有一定了解,对ActiveX控件有一定了解. 一. 前言 ActiveX控件和它的容器程序如 ...

  7. Noip2011提高组总结

    这套题思考的难度比较大,应该说是有四题基础题,一题比较复杂的搜索加模拟,还有一题需要深度思考一下.自己的代码漏洞还是很大,而且思考的时候会遗漏一些情况,这些错误都是致命的,去年Noip的惨败也证实了这 ...

  8. 【浏览器那些基础】Android平台有那些CPU类型

    在编译Android应用的时候,需要配置支持的CPU类型,而目前Android支持的CPU类型包含了ARM和X86,所以在编译前需要指定CPU类型(不同的cpu的特性不一样): X86系列的 expo ...

  9. Android测试TestSuite的执行方法

    public class StartTest extends InstrumentationTestRunner {         public  TestSuite getAllTests() { ...

  10. TCP编程的一个小例子

    TCP程序的服务器端与客户端的流程图 例子:服务器端等待客户端连接,若连接成功,则用户可以通过客户端向服务器端发送任意字符串,服务器端在收到字符串后,输出相关信息,在把接受到的字符串重新发生给客户端. ...