CF484A Bits

题目

https://codeforces.com/problemset/problem/484/A

题解

思路

知识点:贪心,位运算。

每位独立考虑,要使 \(1\) 的数量最大,且答案最小,因此从低位开始遍历,在不超过 \(r\) 的情况下把 \(l\) 每位变 \(1\) 。

(一开始直接写了个结论,但太烦了qwq)

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; /*
bool solve() {
long long l, r;
cin >> l >> r;
long long tmp = l ^ r;
int pos = -1;
while (tmp) {
pos++;
tmp >>= 1;
}
cout << (!~pos || ((1LL << pos) - 1 | r) == r ? r : ((l & r) | (1LL << pos) - 1)) << '\n';
return true;
}
*/ bool solve() {
long long l, r;
cin >> l >> r;
for (int i = 0;i < 63;i++) {
if ((l | (1LL << i)) <= r)l |= (1LL << i);
else break;
}
cout << l << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

CF484A Bits的更多相关文章

  1. CodeForces484A——Bits(贪心算法)

    Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...

  2. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  3. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  5. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  6. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  7. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  8. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  9. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

随机推荐

  1. fpm工具来制作rpm包软件

    第1章 rpm包的制作 1.1 fpm的概念介绍 FPM功能简单说就是将一种类型的包转换成另一种类型 1.1.1.支持的源类型 类型 说明 dir   将目录打包成所需要的类型,可以用于源码编译安装的 ...

  2. 阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境

    一.服务器的购买 1.购买时间点:搞活动的时候.利用学生身份购买 (1)活动:想白嫖一台服务器 双十一,可以在双十一左右,时间提前一点,百度或B站,搜阿里云服务器.腾讯服务器(618可能也有) 一般, ...

  3. liunx对磁盘进行配额限制

    说明,我们给你一个分区进行磁盘配额进行限制 1.首先我们要进行那个分区进行限制,通过修改配置文件加上uquota ,然后再重启一下系统 二.quota 命令用于管理设备的磁盘容量配额,语法格式为 xf ...

  4. Hyperledger Fabric 2.2 学习笔记:测试网络test-network

    写在前面 最近被Hyperledger Fabric折磨,归根结底还是因为自己太菜了qwq.学习路漫漫,笔记不能少.下面的步骤均是基于已经成功搭建了Fabric2.2环境,并且拉取fabric-sam ...

  5. FreeRTOS --(9)任务管理之启动调度器

    转载自 https://blog.csdn.net/zhoutaopower/article/details/107057528 在使用 FreeRTOS 的时候,一般的,先创建若干任务,但此刻任务并 ...

  6. 用浏览器快速开启Docker的体验之旅

    互联网科技发展创造了很多奇迹,比如我今天要提到的 docker 技术就是其一.我很早就关注它(在2015年写过这方面的博客),那会儿还只是一个开源项目,现在已经是一个行业事实标准了,它推动了云原生的变 ...

  7. 微服务生态组件之Spring Cloud OpenFeign详解和源码分析

    Spring Cloud OpenFeign 概述 Spring Cloud OpenFeign 官网地址 https://spring.io/projects/spring-cloud-openfe ...

  8. 腾讯tbs 内存泄露

    一.背景 TBS(腾讯浏览服务)是腾讯提供的移动端webview体验的整套解决方案(https://x5.tencent.com/docs/index.html),可以用于移动端加载doc.xls.p ...

  9. 【面试普通人VS高手系列】Spring中事务的传播行为有哪些?

    一个工作了2年的粉丝,私信了一个比较简单的问题. 说: "Spring中事务的传播行为有哪些?" 他说他能记得一些,但是在项目中基本上不需要配置,所以一下就忘记了. 结果导致面试被 ...

  10. 使用C#跨PC 远程调用程序并显示UI界面

    在项目中有一个需求是需要在局域网内跨PC远程调用一个程序,并且要求有界面显示,调查了一些资料,能实现远程调用的.Net技术大概有PsExec.WMI.Schedule Task. 这三种方式都做了一个 ...