
Sample Input
5
1 10
0 1
1023 1024
233 322
1000000000000000000 1000000000000000000
Sample Output
15 1 2047 511 1000000000000000000
解题思路:
先将数字转换成二进制存储,这时分情况讨论
1,如果两个字符串长度相同时,从高位忘低位遍历,直到遇到不一样的(r=1&&l==0,这个条件也是限制了r>l的这个条件)停止 ,把r之后的位数全变成1
2,如果俩个字符串长度不相同时,r的长度肯定大于l,将r与l相对的位置全部变为1,(r的长度大,r一定是大于L,在这种情况下,让1的个数越多最后结果越大)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N =1010;
int a[N],b[N],c[N];
int main(){
ll y,x;
ll t;
cin>>t;
while (t--) {
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>x>>y;
int lenx=0;int leny=0;
while(x){
if(x&1){
a[lenx++]=1;
}else a[lenx++]=0;
x=x>>1;
}
while(y){
if(y&1){
b[leny++]=1;
}else b[leny++]=0;
y=y>>1;
}
if(lenx<leny){
for(int i=0;i<leny;i++)
c[i]=1;
}else{
ll i;
for(i=leny-1;i>=0;i--){
if(i<lenx&&b[i]==1&&a[i]==0){
break;
}else{
c[i]=b[i];
}
}
for(;i>=0;i--)
c[i]=1;
}
ll ans=0;ll p=1;
for(int i=0;i<leny;i++){
if(c[i])ans+=p;
p*=2;
}
cout<<ans<<endl;
}
return 0;
}
447

被折叠的 条评论
为什么被折叠?



