codeforces - 276D【贪心+数学】

5 篇文章 0 订阅
4 篇文章 0 订阅
     D. Little Girl and Maximum XOR
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integersa 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 integersx and y. The given operation exists in all modern programming languages, for example, in languagesC++ and Java it is represented as "^", inPascal — as «xor».

Input

The single line contains space-separated integers l andr (1 ≤ l ≤ r ≤ 1018).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin, cout streams or the%I64d specifier.

Output

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

Examples
Input
1 2
Output
3
Input
8 16
Output
31
Input
1 1
Output
0


题意:给定 l,r。求l <= a <= b <= r。使得 a^b 最大1

题解:贪心,从最高为往最低位考虑。找到第一个l为0,r为1的位置pos,那么a^b最大值就是 pow(2, pos+1)-1;

           注意预处理出所有的 pow(2, i),或者使用快速幂,不然会精度损失。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
#define rep(i,a,b) for(int i = a;i <= b;++ i)
#define per(i,a,b) for(int i = a;i >= b;-- i)
#define mem(a,b) memset((a),(b),sizeof((a)))
#define FIN freopen("in.txt","r",stdin)
#define FOUT freopen("out.txt","w",stdout)
#define IO ios_base::sync_with_stdio(0),cin.tie(0)
#define mid ((l+r)>>1)
#define ls (id<<1)
#define rs ((id<<1)|1)
#define N 1000+5
#define INF 0x3f3f3f3f
#define INFF 0x3f3f3f3f3f3f3f
typedef long long ll;
const ll mod = 20071027;
const ll eps = 1e-12;
using namespace std;

ll l,r,dp[63];
int main()
{
	dp[1] = 1;
	rep(i, 2, 62)	dp[i] = dp[i-1]*2;
	while(cin >> l >> r){
		int id = -1,bit = 1;
		while(l || r){
			if(r&1 && !(l&1))	id = bit;
			bit++;
			l >>= 1;
			r >>= 1;
		}
		cout << (id == -1 ? 0 : dp[id+1]-1) << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值