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 ...
随机推荐
- IOS 学习笔记(1) 视图UIViewController
1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...
- Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED
Xvfb+YSlow+ShowSlow搭建前端性能测试框架 - 前端技术 | TaoBaoUED Xvfb+YSlow+ShowSlow搭建前端性能测试框架 作者:黑三 | 时间:2010-07-07 ...
- Html.ActionLink(转载)
@Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index" ...
- No.3小白的HTML+CSS心得篇
A--看的东西多了总会出现好多模糊不清的又长的很像的的词语 今天对此进行区别分析下 1. align 与 text-align的区别 align 在W3Cschool中是这样解释的 ----alig ...
- 使用命令部署wsp包,并将其部署到不同的web应用程序
http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-deploy-a-wsp-using-powershell-in-sharepoint- ...
- 【QT相关】文件、目录基础操作
判断目录是否存在: QString proFile(t_path); proFile.append("/dir"); QFileInfo proFileInfo(proFile); ...
- JavaSE学习总结第19天_IO流1
19.01 集合的特点和数据结构总结 HashSet.HashMap.Hashtable判断元素唯一性的方式: 通过对象的hashCode和equals方法来完成元素唯一性 如果对象的hashC ...
- div+css 布局下兼容IE6 IE7 FF常见问题
div+css 布局下兼容IE6 IE7 FF常见问题 收藏 所有浏览器 通用 (市面上主要用到的IE6 IE7 FF)height: 100px; IE6 专用 _height: 100px; IE ...
- centos主机建立ssh互信
ssh-keygen 生成密钥 1.ssh-keygen -t rsa 可以加密和签名 rsa 只能加密不能签名 2.ssh-copy-id -i /root/.ssh/id_rsa.pub USER ...
- Dapper 多数据库优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...