Codeforces 484A - Bits 二进制找1
这题可以根据l, r 在二进制下的长度进行分类。
l 的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r ;
一种是取答案为 (1LL << (rcnt - 1)) - 1 ,意思为比 r 小一位长度,也是 1* 这种样子的数。
l 的长度等于 r 的时候,答案从 l 开始找 , 按位 与 1,同时要满足答案不大于 r 即可。
source code (有参考):
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std; const int INF = 0x3f3f3f3f; int Cal(long long x){
int ret = ;
while (x){
++ret;
x >>= ;
}
return ret;
} int main(){
int n;
long long l, r, ret;
cin >> n;
while (n--){
cin >> l >> r;
int lcnt = Cal(l);
int rcnt = Cal(r);
if (lcnt < rcnt){ //for lcnt != rcnt, answer must like 1* to r
if (r == (1LL << rcnt) - ){
ret = r;
}
else{
ret = (1LL << (rcnt - )) - ;
}
}
else{ //for lcnt == rcnt, answer must be 1* and less than r
for (int i = ; i < rcnt; ++i){
if (((1LL << i) | l) <= r){
l |= (1LL << i);
}
}
ret = l;
}
cout << ret << endl;
}
return ;
}
Codeforces 484A - Bits 二进制找1的更多相关文章
- CodeForces 484A Bits(水题)
A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- codeforces 484a//Bits// Codeforces Round #276(Div. 1)
题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个 ...
- CodeForces 484A Bits
意甲冠军: 10000询价 每次查询输入L和R(10^18) 在区间的二进制输出指示1大多数数字 1个数同样输出最小的 思路: YY一下 认为后几位全是1的时候能保证1的个数多 那么怎样构造 ...
- codeforces 484A A. Bits(贪心)
题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- 【Codeforces 484A】Bits
[链接] 我是链接,点我呀:) [题意] 让你求出l~r当中二进制表示1的个数最多的数x [题解] 最多有64位 我们可以从l开始一直增大到r 怎么增大? 找到l的二进制表示当中0所在的位置 假设i这 ...
- [ 9.22 ]CF每日一题系列—— 484A Bits
Description: 给你一个l,r的区间让你找一个最小的x并且其二进制数要包含最多的1位,输出它的十进制 Solution: 我本来就是贪心,但是贪大了,想1一直往上添加1,但是忘记了0在中间的 ...
- Codeforces - 1020B Badge(找环)
题意: 以每个点为起点,找到第一个出现两次的点 解析: 我是先找出来所有的环 环上的点找出来的肯定是自己 bz[i] = i; 然后去遍历不在环上的点j 如果通过这个点找到一个已经标记的的点i ...
随机推荐
- JS 回到顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浅谈RFID电子标签封装技术
1RFID技术概述 1.1RFID技术概念 RFID是RadioFrequencyIdentification的缩写,即射频识别技术,俗称电子标签.RFID射频识别是一种非接触式的自动识别技术,它通过 ...
- JAVA中SSH面试问题
1.阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器Filter ...
- 如果让莎士比亚、海明威编写JavaScript代码
本文作者Angus Croll是Twitter工程师.JavaScript迷.文学迷,并且非常喜欢作家海明威.他在梦中"梦见"一些名人编写JavaScript代码,不同的作家呈现出 ...
- iOS内存管理 ARC与MRC
想驾驭一门语言,首先要掌握它的内存管理特性.iOS开发经历了MRC到ARC的过程,下面就记录一下本人对iOS内存管理方面的一些理解. 说到iOS开发,肯定离不开objective-c语言(以下简称OC ...
- JavaSE学习总结第09天_面向对象4
09.01 final关键字引入 例: class Fu { public final void show() { System.out.println("访问底层资源"); ...
- 一种解决h5页面背景音乐不能自动播放的方案
场景:微信.浏览器.App 普通解决方案:采用audio标签的autoplay属性 现象: 大部分IOS系统和少部分Android微信不支持自动播放 $解决方案:监听WeixinJSBridgeRea ...
- Nginx 之六: Nginx十万并发优化
操作 操作 Nginx 之六: Nginx十万并发优化
- [LeetCode]题解(python):012-Integer to Roman
题目来源: https://leetcode.com/problems/integer-to-roman/ 题意分析: 这道题是要把在区间[1-3999]的数字转化成罗马数字. 题目思路: 只要知道了 ...
- poj 3608 Bridge Across Islands 两凸包间最近距离
/** 旋转卡壳,, **/ #include <iostream> #include <algorithm> #include <cmath> #include ...