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 ...
随机推荐
- 超酷!!HTML5 Canvas 水流样式 Loading 动画
今天我们要分享另外一款基于HTML5 Canvas的液体流动样式Loading加载动画,这款Loading动画在加载时会呈现液体流动的动画效果,并且由于和背景颜色的对比,也略微呈现发光的动画效果. 效 ...
- 在Java中==的一个坑
观察下面代码,输出结果是什么? public static void main(String[] args) { Integer p = 10000; Integer q = 10000; Syste ...
- Kernel pwn 基础教程之 Heap Overflow
一.前言 在如今的CTF比赛大环境下,掌握glibc堆内存分配已经成为了大家的必修课程.然而在内核态中,堆内存的分配策略发生了变化.笔者会在介绍内核堆利用方式之前先简单的介绍一下自己了解的内核内存分配 ...
- 硬件开发笔记(一):高速电路设计Cadence Aleego软件介绍和安装过程
前言 红胖子软硬通吃的前提的使用AD,涉及到高速电路板,要配合高速硬件工程师,使用Aleegro更合适,遂开启了Aleegro设计电路板学习,过程保存为开发笔记,旨在普及和沟通技术,共同进步,学无 ...
- Linux和kali Linux 介绍
常用的渗透测试平台 CTFTools kali (近亲 Ubuntu) Parrot Security OS PentestBox --由印度人开发,运行在Windows下的渗透测试环境 kali L ...
- 攻防世界-MISC:something_in_image
这是攻防世界高手进阶区的第四题,题目如下: 点击下载附件一,得到一个压缩包,解压后得到一个文件,用010editor打开,搜索flag,即可得到flag 另外一种做法,根据题目包含image,使用ka ...
- 聊聊 node 如何优雅地获取 mac 系统版本
背景 今天突然碰到了一个兼容性需求,需要根据不同 macOS 版本,进行不同的兼容性处理. 没想到看似简单的需求,中间也经历了一番波折,好在最后解决了问题. 在此记录一下解决问题的过程,也方便其他有类 ...
- 【hexo博客搭建】将搭建好的hexo博客部署到阿里云服务器上面(下)
一.部署到阿里云服务器 既然博客也已经成功在本地部署,然后主题也成功安装,接下来就可以部署到服务器上面了,如果你也想要魔改matery主题,可以去各种博客上面找一找大佬的教程,或者联系我,也可以让你少 ...
- 154寻找旋转排序数组中的最小值II
title: 寻找旋转排序数组中的最小值II 题目描述 题目链接:寻找旋转排序数组中的最小值II 解题思路 和上题同理:数组特点有 nums[mid] < nums[right],最小值肯定在m ...
- 爬取豆瓣喜剧类热门TOP60的电影
学习任务:爬取豆瓣喜剧类热门TOP60的电影并保存在douban.txt文件中. 代码示例: import requests url="https://movie.douban.com/j/ ...