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. python基础练习题(题目 求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制)

    day11 --------------------------------------------------------------- 实例018:复读机相加 题目 求s=a+aa+aaa+aaa ...

  2. Amazing!巧用 CSS 视差实现酷炫交互动效

    本文将介绍利用 CSS 实现滚动视差效果的一个小技巧,并且,利用这个技巧来制作一些有意思的交互特效. 关于使用 CSS 实现滚动视差效果,在之前有一篇文章详细描述过具体方案 - CSS 实现视差效果, ...

  3. 记一次sql注入的解决方案

    点赞再看,养成习惯,微信搜索「小大白日志」关注这个搬砖人. 本文在公众号文章已同步,还有各种一线大厂面试原题.我的学习系列笔记. 今天业务提了个模糊查询,一听就知道这种问题有坑,肯定涉及到sql注入, ...

  4. XCTF练习题---CRYPTO---safer-than-rot13

    XCTF练习题---CRYPTO---safer-than-rot13 flag:no_this_is_not_crypto_my_dear 解题步骤: 1.观察题目,下载附件 2.打开后发现是个文件 ...

  5. [AcWing 51] 数字排列

    点击查看代码 class Solution { public: vector<vector<int>> res; vector<vector<int>> ...

  6. .NET MAUI RC2 发布,支持 Tizen 平台

    在.NET多平台应用程序UI(.NET MAUI)RC1之后仅两周,微软已经发布了RC2,并以新的Tizen支持为亮点..NET MAUI是微软对Xamarin.Forms的演变,因为它除了iOS和A ...

  7. 利用撒旦搜索引擎查询ip个数,批量下载ip

    利用撒旦搜索引擎查询ip个数,批量下载ip,使用语言python3.x 批量测试时,为了方便直接撸下ip,所以用python写了个GUI撒旦利用工具,写的不是很好,但能用,最下面有下载. from t ...

  8. HCNP Routing&Switching之MSTP

    前文我们了解了RSTP保护相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16255918.html:今天我们来了解下MSTP相关话题: MSTP技术背 ...

  9. (AAAI2020 Yao) Graph Few-shot Learning via knowledge transfer

    22-5-13 seminar上和大家分享了这篇文章 [0]Graph few-shot learning via knowledge transfer 起因是在MLNLP的公众号上看到了张初旭老师讲 ...

  10. 【多线程】JUC版的CopyOnWriteArrayList

    CopyOnWriteArrayList CopyOnWriteArrayList适合于多线程场景下使用,其采用读写分离的思想,读操作不上锁,写操作上锁,且写操作效率较低. CopyOnWriteAr ...