【CF484A】Bits

题目

题意翻译

  • n n n组询问,每次给出一个区间 l , r l, r l,r,你需要输出在这个区间内二进制表示中1的个数最多的数

  • 如有多个答案,输出最小的那个

  • ( n ≤ 1 0 4 , 0 ≤ l , r ≤ 1 0 18 ) (n \leq10^4, 0\leq l, r \leq10^{18}) (n104,0l,r1018)
    效果
    nn组询问,每次给出一个区间l, rl,r,你需要输出在这个区间内二进制表示中1的个数最多的数

如有多个答案,输出最小的那个

(n \leq10^4, 0\leq l, r \leq10^{18})(n≤10
4
,0≤l,r≤10
18
)
题目描述
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .

You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.

输入输出格式
输入格式:
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .

You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.

输出格式:
For each query print the answer in a separate line.

输入输出样例
输入样例#1: 复制
3
1 2
2 4
1 10
输出样例#1: 复制
1
3
7
说明
Let’s denote as the number of bits set (‘1’ bits) in the binary representation of the non-negative integer x x .

You are given multiple queries consisting of pairs of integers l l and r r . For each query, find the x x , such that l<=x<=r l<=x<=r , and is maximum possible. If there are multiple such numbers find the smallest of them.

思路

很明显是贪心。

我们很明显发现在位权小的位置放1能够使得最终结果最少。

做法:

初始的时候是1

于是我们就从位0到inf放1,如果>r就break

代码

#include <bits/stdc++.h>
using namespace std ;
#define int long long 
int n,l,r ;
signed main(){
    scanf("%lld",&n) ;
    while(n--){
        scanf("%lld%lld",&l,&r) ;
        for (int i=0;;i++){
            if ((l|(1ll<<i))>r) break ;
            l|=(1ll<<i) ;
        } 
        printf("%lld\n",l) ;
    } 
    return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值