CF484A Bits
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的更多相关文章
- CodeForces484A——Bits(贪心算法)
Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...
- [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 ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- uva12545 Bits Equalizer
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
随机推荐
- python基础练习题(题目 斐波那契数列II)
day16 --------------------------------------------------------------- 实例024:斐波那契数列II 题目 有一分数序列:2/1,3 ...
- pgpool-II 4.3 中文手册 - 入门教程
本章解释了如何开始使用 Pgpool-II. 安装 在本节中,我们假设您已经安装了 Pgpool-II 与 PostgreSQL 集群. 你的第一个复制(Replication) 在本节中,我们将解释 ...
- WebSocket 协议详解
一.WebSocket 协议背景 早期,在网站上推送消息给用户,只能通过轮询的方式或 Comet 技术.轮询就是浏览器每隔几秒钟向服务端发送 HTTP 请求,然后服务端返回消息给客户端. 轮询技术一般 ...
- docker:compose安装
compose:独立于docker的程序,可以做多容器app 安装docker-compose: 下载docker-compose文件: curl -L https://github.com/dock ...
- mybatis 查询返回的类型中字段类型为 List<xx>
基本类型数组 mapper.xml <resultMap id="xxDtoResultMap" type="com.xx.xxDto"> < ...
- 解决windows server 2008r2服务器自动关机
问题 具体表现就是系统自动关机,网上说是开机后2小时就会自动关机 系统版本: 解决 PsTools下载 解压:PSTools.zipg,如解压到C:\PSTools目录下 执行如下命令,打开注册表 W ...
- JavaScript数组操作常用方法
@ 目录 数组基础遍历方法. for for of for in 数组的基础操作方法. push:尾部追加元素 pop:尾部移出元素 unshift:头部追加元素 shift:头部移出元素 splic ...
- python初识数据类型(字典、集合、元组、布尔)与运算符
目录 python数据类型(dict.tuple.set.bool) 字典 集合 元组 布尔值 用户交互与输出 获取用户输入 输出信息 格式化输出 基本运算符 算术运算符 比较运算符 逻辑运算符 赋值 ...
- 每天一个 HTTP 状态码 201
201 Created 201 Created 表示客户端的请求已经成功完成,结果是创建了一个新资源,通常用于响应「增删改查」里的「增」.如果是严格按照 RESEful style 的 API,那么当 ...
- Python搜索书名获取整本资源_笔趣阁
前言 偶然一天把某项目文档传到手机上,用手机自带的阅读器方便随时拿出来查阅.看着我那好久没点开的阅读器,再看着书架上摆着几本不知道是多久之前导入的小说. 闭上眼,我仿佛看到那时候的自己.侧躺着缩在被窝 ...