CodeForces 276D – Little Girl and Maximum XOR 贪心
整整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 a, b (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 a, b (l ≤ a ≤ b ≤ r).
CodeForces 276D – Little Girl and Maximum XOR 贪心的更多相关文章
- Codeforces 276D Little Girl and Maximum XOR
题意:给范围l,r选两个数亦或最大是多少. 思路:找到第一个l和r二进制下不相同的位置i,然后答案就是2^(i+1)-1,因为一个取0一个取1之后,后面的位置全部选1和全部选0,就是这样:011111 ...
- 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 ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- CodeForces 280B Maximum Xor Se
题目链接:http://codeforces.com/contest/280/problem/B 题目大意: 给定一个由n个数组成的一个序列,s[l..r] (1 ≤ l < r ≤ n)代表原 ...
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...
- CodeForces 702 A Maximum Increase (贪心,高效算法)
题意:给定 n 个数,问你连续的最长的序列是几个. 析:从头扫一遍即可. 代码如下: #include <cstdio> #include <string> #include ...
- Codeforces 870C Maximum splitting (贪心+找规律)
<题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
随机推荐
- Git Command Summary (Updated)
取得Git仓库 初始化一个版本仓库 git init Clone远程版本库 git clone git@xbc.me:wordpress.git 添加远程版本库origin,语法为 git remot ...
- Altium designer 10如何设置标题栏
一.修改设置 1.执行Design-Document Option,打开文档属性对话框,设置其中title等参数. 2.执行Place-Text String,按TAB键,将Text属性中设置为&qu ...
- HDU 1157 Who's in the Middle
#include <cstdio> #include <algorithm> using namespace std; int main() { int n; while(sc ...
- AlertDialog弹出时背景明暗程度调整
今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...
- Clojure绘制UML
简单介绍 使用Clojure封装了Graphviz的使用.眼下主要实现了UML的绘制 使用 以命令模式的UML为例,演示cdraw的使用 安装Graphviz cdraw是对Graphviz的简单封装 ...
- 开始QT+OpenCV学问
最近一个月.由于超声造影软件工具做.因此,开始接触OpenCV.使用OpenCV的话.除了图像处理,其它功能都非常弱.所以又開始学习MFC. 从原先的.net C#编程环境一下变成MFC还真有点不习惯 ...
- 信号量多-threaded同步Semaphore
Semaphore它是JDK1.5一个实现后,外面有个办法同步.Semaphore能够保持其当前的线程接入号码.并提供了一个同步机制. 采用Semaphore时,可以用相同的对资源的访问进行控制的线程 ...
- c基础总结
机器大小端判断: #include <stdio.h> typedef union{ char x; int i; }un; int main() { un tt; tt.i = ; ) ...
- 快速构建ASP.NET MVC Admin主页
前言 后台开发人员一般不喜欢调样式,搞半天样式出不来,还要考虑各种浏览器兼容,费心费力不讨好,还好互联网时代有大量的资源共享,避免我们从零开始,现在就来看怎么快速构建一个ASP.NET MVC后台管理 ...
- webviewactivity
WebView注意点,注释里有说明 package com.example.suneyaenews; import com.example.http.HttpThread; import androi ...