Codeforces 484A. Bits 贪心

本文探讨了一种高效的方法来解决多个查询中找到非负整数x的问题,该整数满足特定的位数条件,并在给定的范围内达到最大可能的位数。通过实例分析,展示了如何通过迭代和位运算技巧,实现快速且准确地找到符合条件的最小x值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



A. Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

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

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Sample test(s)
input
3
1 2
2 4
1 10
output
1
3
7
Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102


/**
 * Created by ckboss on 14-11-7.
 */
import java.util.*;

public class CF484A
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T_T = in.nextInt();
        while (T_T-- > 0) {

            long l = in.nextLong(), r = in.nextLong();

            long low=0;
            while(true) {

                int mid = -1;
                long low_leve=-1;
                for (int i = 0; i <= 60; i++) {
                    long temp=(1L << i) - 1L;
                    if(temp>r) break;
                    if ( temp >= l && temp <= r) {
                        mid = i;
                    }
                    if(temp < l){
                        low_leve=temp;
                    }
                }

                if (mid != -1) {
                    System.out.println((1L << mid) - 1L+low);
                    break;
                }
                else{
                    low_leve++;
                    low+=low_leve;
                    l-=low_leve; r-=low_leve;
                }
            }
        }
    }
}




### Codeforces Round 995 Div. 3 比赛信息 Codeforces是一个面向全球程序员的比赛平台,Round 995 (Div. 3) 是该平台上的一场编程竞赛。这场比赛旨在为不同水平的选手提供挑战机会,特别是针对评级较低的新手和中级选手设计了难度适中的题目[^1]。 ### 题目解析 #### A. Preparing for the Olympiad 这道题目的背景设定是在准备奥林匹克竞赛的过程中遇到的问题。给定n个学生以及m本书籍,每本书都有一定的数量副本。目标是分配书籍使得每个学生都能获得至少一本书,并且尽可能让更多的书被分发出去。通过计算可以得出最少需要多少额外购买的书籍来满足条件。对于这个问题,可以通过简单的贪心算法解决:先对学生按需求升序排列,再依次尝试用现有的书籍去匹配这些最小的需求直到无法继续为止;最后统计未能得到满足的学生数目即为所需新增购入的数量。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } sort(a.begin(),a.end()); long long sum = accumulate(a.begin(), a.begin()+min(m,n), 0LL); // 计算前几个数之和 cout << max(0ll,sum-(long long)m*(min((long long)n,(long long)m)-1)/2); } ``` ### 参赛攻略 为了在这类比赛中取得好成绩,建议提前熟悉比赛环境并练习过往真题。理解官方文档中给出的例子非常重要,可以帮助快速抓住解法要点。另外,在实际编码之前花时间思考最优解决方案往往能节省不少调试的时间。合理安排做题顺序也很关键&mdash;&mdash;通常可以从最简单的小题入手积累信心,逐步向更难的大题过渡。保持冷静的心态面对突发状况同样不可或缺,比如网络波动或者意外错误等情形下仍需镇定自若地处理问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值