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
5
1 10
0 1
1023 1024
233 322
1000000000000000000 1000000000000000000
Sample Output
15
1
2047
511
1000000000000000000
Source
2016年中国大学生程序设计竞赛(合肥)-重现赛(感谢安徽大学)
简单水题
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=100+10;
int a[N],b[N];
int erjin(int x,int *m)
{
int l=0;
while(x)
{
m[l++]=x%2;
x>>=1;
}
return l;
}
signed main()
{
int l,r;
int t;
scanf("%lld",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%lld%lld",&l,&r);
int d1=erjin(l,a);
int d2=erjin(r,b);
int k=0;
for(int i=d2-1;i>=0;i--)
{
if(a[i]!=b[i])
{
k=i+1;
break;
}
}
if(k)
k=1LL<<k,k--;
printf("%lld\n",r|k);
}
return 0;
}