【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干:

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and  is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Examples

Input

3
1 2
2 4
1 10

Output

1
3
7

Note

The binary representations of numbers from 1 to 10 are listed below:

题目大意:

我们定义表示数x在二进制表示下所有数位中1的个数。

现在给定两个整数 l 和r。希望找到一个 x,满足 l ≤ x ≤ r,并且的值最大。如果有多种方案输出最小的一种。

解题报告:

   贪心处理,并且他这个:If there are multiple such numbers find the smallest of them.也给了提示,需要从小到大贪心。对于这个题我们不需要首先关注这个值是多少,而是关注值中有多少个1(因为首先输出1最多的数字)。我们可以采用上下界贪心的思路,最小的值肯定是 l ,答案肯定不会少于l,并且答案首先最少就是popcount( l ),然后我们依次增加答案并且保证构造的是最小值,怎么做到呢?就是从低位到高位按位增加一个1。依次类推 就完事了。注意写法。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,x,y,a[MAX],ans;
int main() 
{ll n,l,r;cin>>n;while(n--) {scanf("%lld%lld",&l,&r);ll ans = 0;ll bit = 1;while(l<=r) {ans = l;l |= bit;bit<<=1;}printf("%lld\n",ans);} return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440764.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485B】Valuable Resources (贪心,水题,几何相关)

题干&#xff1a; Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Lets suppose that your task is to build a square city. The world map uses the Car…

php测试插入,php – 使用Symfony测试数据库插入

今天是个好日子,在过去的几天里,我一直在研究测试驱动开发,并决定我也需要学习它.虽然我无法弄清楚如何准确地做到这一点.我的项目依赖于Symfony2.1.6框架和Doctrine,所以我有一些需要填充的数据库表.Book (1,n) – (0,n) Genre现在,如果我想插入一个类型记录,我首先需要编写一…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…

【CodeForces - 1020C】Elections (枚举投票数,贪心)

题干&#xff1a; As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know th…

java 制作快捷键,Java快捷键制作

Java快捷键制作用过VB的知道按下F5是运行窗体&#xff0c;用过Photoshop的知道按下Tab是隐藏所有面板&#xff0c;你的Java程序也想有这个功能么&#xff1f;那么希望这篇文章对您有所帮助。子菜单类JMenuItem有一个方法不知道你注意过没有:setAccelerator。打开帮助文件对这个…

【CodeForces - 1020A】New Building for SIS(模拟)

题干&#xff1a; You are looking at the floor plan of the Summer Informatics Schools new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get …

matlab 实验5,MATLAB实验5报告.doc

实验课程名称&#xff1a;《数学软件与数学实验》实验项目名称实验五&#xff1a;线性规划问题求解实验成绩实 验 者王宗德专业班级数学1504组 别同组者实验日期16年5月一、实验目的1. 掌握用MATLAB优化工具箱求解线性规划问题的方法&#xff1b;2. 练习建立实际问题的线性规划…

【CodeForces - 1020B】Badge(模拟,图,环)

题干&#xff1a; In Summer Informatics School, if a student doesnt behave well, teachers make a hole in his badge. And today one of the teachers caught a group of nn students doing yet another trick. Lets assume that all these students are numbered from …

Matlab样条拟合曲面,样条曲面拟合及其Matlab实现

1符号和定义先讨论一元情形,给定区间[a,b]的一个分划,ax00,x(xi,xik)0,x[xi,xik],i-k1,…,n-1(iii)若xjixijh,则Ni,k(x)k(x-xih-k12)其中k(x)k1j0(-1)jCjk1(xk12-j)k/k!为以xjj-k12(j0,1,…,k1)为结点的k次等距B样条.Ni,k(x)的其他性质参见[1,2,3,4]满足(3)的k次样条插值曲线s(…

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

php postgresql多条,PHPPostgreSQL函数列表 - phpStudy

echo $r["fun"]. .$r["title"]; ?>PostgreSQL函数列表pg_close - 关闭一个 PostgreSQL 连接pg_connect - 打开一个 PostgreSQL 连接pg_convert - 将关联的数组值转换为适合 SQL 语句的格式。pg_copy_from - 根据数组将记录插入表中pg_copy_to - 将一个…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树)

题干&#xff1a; The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. Theyre planning to build some highways so that it…

js获取php单选按钮值,js如何获取radio单选框选中的值

js中一般使用遍历的方法获取radio被选中的值&#xff0c;遍历判断每个Radio是否被选中,如果是,再取其值.123function usubmit(action){var radionum document.getElementById("userlist").userid;for(var i0;iif(radionum[i].checked){userid radionum[i].value}}w…

【EOJ Monthly 2019.02 - F】方差(数学,前缀和,积的前缀和)

题干&#xff1a; 单测试点时限: 2.0 秒 内存限制: 256 MB “放弃不难&#xff0c;但坚持一定很酷。” QQ 小方已经在体育馆苦练一天射箭了&#xff0c;但他还在坚持。 QQ 小方每天都要在朋友圈晒自己的训练记录。他一共进行了 n 次射箭&#xff0c;成绩分别是 x1,x2,⋯,x…

oracle gtx,ORACLE

Category Archives: ORACLE所有Oracle技术文章看到群里有人提出一个需求&#xff0c;一张表数据量很大&#xff0c;只想导出其中一部分列。 无论是老版本exp还是数据泵expdp&#xff0c;Oracle都提供了QUERY的功能&#xff0c;这使得查询表中部分记录的功能可以实现&#xff0c…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…