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. ArcGIS建筑物简化和建筑物群聚合算法实验

    一.下载OSM数据 首先从OpenStreetMap官网下载我们需要的实验数据,这里我选择清华和北大校园作为本次实验数据 二.数据处理 将我们下载的实验数据导入ArcGIS.由于OSM数据是.osm格 ...

  2. vsphere部署OVF虚拟机提示未能部署OVF软件包

    一.从vshere平台导出OVF,准备导入到另一个vsphere平台提示:传输入失败:Error transferring file to https://172.22.1.85/nfc/5267db ...

  3. 攻防世界-MISC:Aesop_secret

    这是攻防世界高手进阶区的的第九题,题目如下: 点击下载附件一,得到一个压缩包,解压后得到一张GIF动图,找个网站给他分解一下,得到如下图片 不知道是什么意思,所以就跑去看WP了,用010editor打 ...

  4. 在windows上安装elasticsearch7.6

    在windows上安装elasticsearch7.6 安装Java1.8 下载Java1.8 提取码:yi0c 链接:https://pan.baidu.com/s/1mNd2Yg-k6ob91bO ...

  5. line-height和height关系

    如图所示,line-height = font-size + 上下本行距.上下半行距总是相等.font-size居于中间.当font-size值固定时,line-height越大,半行距越大.所以当l ...

  6. 论文解读(ClusterSCL)《ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs》

    论文信息 论文标题:ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs论文作者:Yanling Wang, Jing ...

  7. 一个关于 useState 的误解

    一个关于 useState 的误解 本文写于 2020 年 11 月 17 日 前两天有人问了我一个问题,他有一段这样的代码: function App() { const [n, setN] = u ...

  8. 被迫开始学习Typescript —— interface

    一开始以为,需要使用 class 来定义呢,学习之后才发现,一般都是使用 interface 来定义的. 这个嘛,倒是挺适合 js 环境的. 参考:https://typescript.bootcss ...

  9. 从NSSRound#1学到了什么

    sql_by_sql 二次注入: 更改密码的功能形如: update user set password='%s' where username='%s'; 的语句就可以存在二次注入,即假设有个adm ...

  10. Java学习,利用IDEA开发工具连接redis

    Idea连接redis及Jedis数据操作 注意是否开启了redis服务!!! 1.打开虚拟机终端,查看虚拟机防火墙是否关闭 查看防火墙当前状态命令: $sudo ufw status 我的是默认关闭 ...