codeforces 276D Little Girl and Maximum XOR(区间最大异或值--技巧)【模板】

来源:互联网 发布:事业单位会计软件 编辑:程序博客网 时间:2024/06/01 16:50

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».

Input

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

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

Output

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

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

【题解】

 刚开始一看到数据范围惊呆了,但是最后仔细思考以后发现是有技巧可言的,首先,异或值要为1,那么两个数的对应2位进制位要不同,而异或值要最大,则二进制的高位要尽可能的为1,所以这就是切入点,从给定的区间上下限入手(l和R),从l和r二进制的最高位开始比较,如果出现对应位异或值位1,就从该处开始,低位都置为1,此时其表示的数就是最大异或值了。


【AC代码】

#include<cstdio>#include<algorithm>#include<cstdio>#include<math.h>#include<string.h>#include<iostream>using namespace std;typedef __int64 ll;ll l,r;int main(){    while(~scanf("%I64d%I64d",&l,&r))    {        ll max_xor = 0;        int i = 61;        for(;i>=0;--i)        {            if(l&(1LL<<i) ^ r&(1LL<<i)) break;        }        for(;i>=0;--i)        {            max_xor|= 1LL<<i;        }        printf("%I64d\n",max_xor);    }    return 0;}



阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿仙人掌的刺怎么办 仙人掌刺扎进皮肤里怎么办 被仙人掌的小细绒刺扎到怎么办 值机截止了怎么办 社保卡消磁了怎么办 住宾馆没身份证怎么办 社保卡电话错误怎么办 没住过酒店怎么办 上海合规网约车证件怎么办 取票没有身份证怎么办 买车票没有身份证怎么办 登机身份证丢了怎么办 飞机没带身份证怎么办 上飞机没带身份证怎么办 机场没带身份证怎么办 儿童坐飞机没有户口本怎么办 不需要行李托运化妆品怎么办 坐飞机被限制了怎么办 坐飞机没买保险怎么办 飞机起飞时难受怎么办 婴儿坐飞机没座位怎么办 转机行李不直达怎么办 儿童自己坐飞机怎么办手续 两岁宝宝发烧怎么办 网上购票待核验怎么办 老人坐飞机找不到登机口怎么办 儿童不能值机怎么办 飞机上旅客刁难怎么办 飞机上婴儿哭闹怎么办 飞机上婴儿啼哭怎么办 飞机儿童陪护手续怎么办 孕妇感冒鼻子堵怎么办 孕妇用了眼药水怎么办 孕妇用眼药水了怎么办 错过了登机时间怎么办 过了登机时间怎么办 孩子不喜欢做作业怎么办 宝宝腿不一样粗怎么办 一个小腿肿了怎么办 左右小腿不一样粗怎么办 小孩不敢自己睡怎么办