码迷,mamicode.com
首页 > 其他好文 > 详细

CF169D2 D – Little Girl and Maximum XOR 贪心

时间:2014-05-12 20:05:45      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

解题思路:

经过打表可得规律答案要么是0 要么是2的N次

要得到最大的XOR值,其值一定是2的N次

即在 l 和 r 的二进制中,从左到右遍历过去,如果碰到 l 为 1 r 为 0 

则可说明在『l , r】中存在 1000000000 和 0111111111 可得到最大XOR值为2的N次

 

PS:不会存在首先出现 l 为 0 r 为 1 的情况,因为 l < r 

 

 

bubuko.com,布布扣
#include<stdio.h>
#include<math.h>
int main(){
    long long x,y,ans=0;
    int i;
    while(EOF != scanf("%lld%lld",&x,&y)){
        ans = 0;
        for(i=63;i>=0;i--)
            if((x&((long long)1<<i))^(y&((long long)1<<i))) break;
        ans = pow(2,i+1)-1;
        printf("%lld\n",ans);
    }
    return 0;
}
bubuko.com,布布扣

题目描述

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 bubuko.com,布布扣 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 bubuko.com,布布扣 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 bubuko.com,布布扣 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 bubuko.com,布布扣 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 bubuko.com,布布扣 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 bubuko.com,布布扣 for all pairs of integers ab (l?≤?a?≤?b?≤?r).

CF169D2 D – Little Girl and Maximum XOR 贪心,布布扣,bubuko.com

CF169D2 D – Little Girl and Maximum XOR 贪心

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/wushuaiyi/p/3722179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!