整整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. 织梦dedecms|文章页通用标签

    当前位置: {dede:field name='position'/}上一页: {dede:prenext get='pre'/}下一页:    {dede:prenext get='next'/}收 ...

  2. mabatis mapper开发规范

    定义一个mapper接口 package mapper; import pojo.User; public interface UserMapper { public User getUser(int ...

  3. Protel99se教程六:创建原理图元件库

    在我们平时使用protel99se进行电路以及PCB设计的时候,系统自带的元件库和PCB封装库,只有一小部分,大部份元件的元件库以及封装库,我们都需要自己制作,使用protel99se,我们可以很容易 ...

  4. 发一个讨论帖,如果结果被采纳的话可以给一份adb 代码,以及我封装的ADBLIB

    如何在手机没有root 的情况下,获取系统的一些文件,比如 /data/data/xxxx 目录下的文件. 有任何想法的请说出来.

  5. Objective-c Category(类别)

    category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...

  6. uva 1561 - Cycle Game(推理)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...

  7. JS,JQuery各种获取屏幕的宽度和高度

    JQuery: $(document).ready(function(){ alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).h ...

  8. javascript 横向下拉菜单演示

    <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head><me ...

  9. 分页:T-SQL存储过程和EF存储过程的使用

    首先准备好分页的T-SQL语句: create proc usp_activityFenYe @pageIndex int, @pageSize int, @pageCount int output ...

  10. Visual Studio Tools for Unity安装及使用

    Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...