码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • CodeForces484A——Bits(贪心算法)



    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
    题目大意:

        给定L,R,输出X,X为[L,R]中化为二进制之后一的个数最多的那个数,(同时存在多个解,输出最小的那个)。

    解题思路:

        设L,R的从高位开始有t1位相同,那么根据常识,x的前t1位(从高位开始数)必然也与LR相同。又因为L<=R,那么t1+1位不同,必然是L为0,R为1。

        此时将x的t1+1位赋值为0,后面所有为都为1。那么求出的X: L<=x

        存在特殊情况:R换成二进制之后全部为1,所以要加一个判断。判断x在t1+1位为1的时候是否绝对大于R,绝对大于的话,就将t1+1赋值为0;反之,赋值为1。

    Code:

     6  ************************************************************************/
     7 
     8 #include
     9 #include
    10 #include
    11 #include
    12 #include
    13 #include
    14 #include
    15 #include
    16 #include
    17 #include
    18 #include
    19 #include
    20 #include
    21 #include
    22 #define MAXN 100000
    23 #define LL long long
    24 using namespace std;
    25 int main()
    26 {
    27     LL a,b,ans;
    28     int T,k;
    29     cin>>T;
    30     while (T--)
    31     {
    32         ans=0;
    33         cin>>a>>b;
    34         int i;
    35         bool flag=0;
    36         for (i=60; i>=0; i--)
    37         {
    38             if (flag)
    39             {
    40                 ans+=1LL<b)
    56             ans=ans^(1LL< 
    

  • 相关阅读:
    【Python】成功解决TypeError: list indices must be integers or slices, not float
    HR常用的人才测评工具,测什么,怎么测?
    从零开始:Django项目的创建与配置指南
    Java多线程基础(创建、使用,状态)——Java第九讲
    树莓派官方系统自带的gpiozero库教程
    如何学好C++?学习C和C++的技巧是什么?
    java+python+nodejs+vue音乐网站管理系统ssm
    软件测试需求人才越来越多,走上测试道路的人却越来越少?
    文心一言 VS 讯飞星火 VS chatgpt (117)-- 算法导论10.3 2题
    【shiro】Apache Shiro 1.2.4 反序列化漏洞(CVE-2016-4437 )
  • 原文地址:https://blog.csdn.net/m0_72431373/article/details/127994239
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号