整整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. java ee环境搭建

    1.安装java(tm)2 platform 2.下载安装Java EE SDK 版本:Java Platform,Enterprise Edition 7 SDK (with JDK 7u45) 下 ...

  2. 调不尽的内存泄漏,用不完的Valgrind

    调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-X.X.X.tar.bz2 2. 解压安装包:tar –jxvf ...

  3. Python经常使用第三方工具、库、骨架

    Python ImagingLibrary(PIL):它提供强大的图形处理的能力,并提供广泛的图形文件格式支持.该库能进行图形格式的转换.打印和显示.还能进行一些图形效果的处理,如图形的放大.缩小和旋 ...

  4. SAP HANA 开发者中心(Developer Center)入门指南

  5. C#中泛型、程序集一些基本运用(Fifteenth Day)

    今天主要在学习了泛型和程序集以及一些细碎的知识的运用.下面我就把今天所学的总结一下. 理论: 泛型: * 英文名字是Generic,可以让多个类型共享一组代码,泛型允许我们声明类型参数化,可以用不同的 ...

  6. 前端CSS规范大全

    一.文件规范 1.文件均归档至约定的目录中(具体要求以豆瓣的CSS规范为例进行讲解): 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用 ...

  7. unity之uv贴图画圆弧,圆弧面,不规则图形

    由于最近一直没有时间,所以这篇博客一直没发,下面我说说uv画圆弧,圆面,不规则面拼接. 先来两张效果图 图截的不咋滴,凑合着看吧,画圆弧主要用的贝塞尔曲线画的,我感觉这个比较简单,当然大家也可以使用圆 ...

  8. mysql支持emoji解决办法

    mysql显示不了emoji表情或者显示??,原因这里不解释,直接说解决办法.(主要就是修改utf8mb4) 1.修改表 ALTER TABLE `TABLE_NAME` CHARACTER SET ...

  9. nginx根据条件跳转+跳转规则

    好的参考博文: nginx rewrite规则 自己写的时候参考这两个 Nginx 伪静态Rewrite,重定向Location配置总结(转) nginx rewrite规则语法 一.输入子目录跳转 ...

  10. [LeetCode]题解(python):014-Longest Common Prefix

    题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. ...