最大的位或(位运算)


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Problem Description
B君和G君聊天的时候想到了如下的问题。
给定自然数l和r ,选取2个整数x,y满足l <= x <= y <= r ,使得x|y最大。
其中|表示按位或,即C、 C++、 Java中的|运算。
 

Input
包含至多10001组测试数据。
第一行有一个正整数,表示数据的组数。
接下来每一行表示一组数据,包含两个整数l,r。
保证 0 <= l <= r <= 1018
 

Output
对于每组数据输出一行,表示最大的位或。
 

Sample Input
 
  
51 100 11023 1024233 3221000000000000000000 1000000000000000000
 

Sample Output
 
  
15120475111000000000000000000
 

Source
 

Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:   6297  6296  6295  6294  6293 
 

    Statistic |  Submit |  Discuss | Note

位运算

移位运算符有双目移位运算符:<<(左移)和>>(右移)

左移运算是将一个二进制位的操作数按指定移动的位数向左移位,移出位被丢弃,右边的空位一律补0。右移运算是将一个二进制位的操作数按指定移动的位数向右移动,移出位被丢弃,左边移出的空位或者一律补0,或者补符号位

例如,设无符号短 整型变量a为0111(对应二进制数为0000000100010001),
则:a<<3 结果为0888(对应二进制数为0000100010001000),a不变(即运算式本身为一个值,但不改变被操作对象值)
a>>4 结果为006 (对应二进制数为0000000000010001),a不变
题解 就是先把l和r都转换为二进制,然后开始遍历,遇到不同后面全部补1即可。需要注意long long的问题。
模拟代码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int a[100005],b[100005];
int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        long long l,r,ans=0;
        cin>>l>>r;
        if(l==r)
        {
            cout<<l<<endl;
            continue;
        }
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            int i=0,j=0;
            while(l!=0)
            {
                int t=l%2;
                a[i++]=t;
                l/=2;
            }
            while(r!=0)
            {
                int t=r%2;
                b[j++]=t;
                r/=2;
            }
            for(int k=64; k>=0; k--)
            {
                if(a[k]==b[k])
                {
                    if(a[k]==1)
                        ans+=(long long)pow(2,k);
                }
                else
                {
                    ans+=(long long)pow(2,k+1)-1;
                    break;
                }
            }
            cout<<ans<<endl;
    }
    return 0;
}



位运算代码

思路 将 one=1,讲one一位一位的左移,右边一个个的补零,让 a | one使其a中 0 变成 1 并且小于b

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int main()
{
    int t;
    scanf("%d",&t);
    ll a,b;
    ll one=1;
    while (t--)
    {
        scanf("%lld%lld",&a,&b);

        int pos = 0;
        while ((a|(one<<pos)) <= b)
        {
            a |= one << pos;
            pos++;
        }
        printf("%lld\n",a|b);
    }
    return 0;
}

 
 




猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80559557