Codeforces Round #276 (Div. 1) A. Bits

题目: LINK

给n个询问,每个询问给出l, r 求出[l,r]内数字写成2进制形式,含有1的个数最多的数字是哪个,如果有多个输出值最小的那个。
F(l, r) 定义为[l, r]区间内题目要求的结果
if l == r , F(l, r) = l; 
2^x为不超过r的最大值 如果 l< 2^x <= r ,那么最大值为2^(x+1)-1 (如果这个值在范围内的话),否则为2^x - 1; 
如果2^x <= l, 最大值为 F(l - 2^x, r - 2^x) + 2^x

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
typedef __int64 LL; 
int n; 
LL l, r; 
LL sol(LL l, LL r) {
    LL ret = 0; 
    if(l == r )  return l;  
    LL tmp = 1; 
    while(tmp <= r)  tmp <<= 1; 
    tmp >>= 1; 
    LL nn = tmp << 1; nn --; 
    if(tmp <= l) {
        return sol(l - tmp, r - tmp) + tmp; 
    }
    else if(nn >= l && r >= nn)  return nn;  
    else  return tmp-1;  
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin); 
#endif // ONLINE_JUDGE
    scanf("%d", &n); 
    while(n--) {
        cin>>l>>r; 
        cout<<sol(l, r)<<endl; 
    }
    return 0; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值