码农知识堂 - 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< 
    

  • 相关阅读:
    py15_Python 流程控制之 for-else 和 range() 步长以及 break/continue/return 跳转语句
    K8S环境搭建
    Dubbo(五):Dubbo 2.7 服务导出引入调用
    图片如何转换成PDF格式?教你一招快速转换
    AutoJs7打包薅羊毛时间版
    是谁在造谣杭州取消直播带货?
    1.3 特殊的矩阵乘法
    2023年眼镜行业分析(京东眼镜销量数据分析):市场规模同比增长26%,消费需求持续释放
    Hdu2022 多校训练(5) BBQ
    提升预算管控精度,助力保险资管协会财务管理数字化转型
  • 原文地址:https://blog.csdn.net/m0_72431373/article/details/127994239
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号