codeforces484A
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
3
1 2
2 4
1 10
1
3
7
Note
The binary representations of numbers from 1 to 10 are listed below:
110 = 12
210 = 102
310 = 112
410 = 1002
510 = 1012
610 = 1102
710 = 1112
810 = 10002
910 = 10012
1010 = 10102
sol:因为二进制一些奇奇怪怪的东西,我们可以得到以下的乱搞,先一位位加上去加到比R大了为止,然后要减到恰好大于等于L,所以尽可能减大的,并没有减过的位
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
int Q;
bool Arr[];
//const ll a[65]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215,33554431,67108863,134217727,268435455,536870911,1073741823,2147483647,4294967295,8589934591,17179869183,34359738367,68719476735,137438953471,274877906943,549755813887,1099511627775,2199023255551,4398046511103,8796093022207,17592186044415,35184372088831,70368744177663,140737488355327,281474976710655,562949953421311,1125899906842623,2251799813685247,4503599627370495,9007199254740991,18014398509481983,36028797018963967,72057594037927935,144115188075855871,288230376151711743,576460752303423487,1152921504606846975,2305843009213693951};
int main()
{
int i,j;;
R(Q);
while(Q--)
{
ll L=read(),R=read(),ans=,oo=-;
for(i=;ans<R;i++)
{
++oo;
ans+=(1ll<<(oo));
}
memset(Arr,,sizeof Arr);
while(ans>R)
{
for(j=oo;~j;j--) if(!Arr[j])
{
if(ans-(1ll<<j)>=L)
{
ans-=(1ll<<j); Arr[j]=; break;
}
}
}
Wl(ans);
}
return ;
}
/*
Input
3
1 2
2 4
1 10
Output
1
3
7
*/
codeforces484A的更多相关文章
- CodeForces484A Bits(贪心)
CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...
- CodeForces484A——Bits(贪心算法)
Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...
随机推荐
- c#简单的io
读取路径判断文件是否存在,进行删除或者创建 简单的io using System; using System.Collections; using System.Collections.Generic ...
- echarts 图例显示到右边
原: legend: { data:['同龄普通孩子','已具备技能','已泛化技能','已掌握技能','学习中'] }, 改: legend: { data:['同龄普通孩子','已具备技能','已 ...
- sklearn 数据预处理1: StandardScaler
作用:去均值和方差归一化.且是针对每一个特征维度来做的,而不是针对样本. [注:] 并不是所有的标准化都能给estimator带来好处. “Standardization of a dataset i ...
- 比官方文档更易懂的Vue.js教程!包你学会!
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由蔡述雄发表于云+社区专栏 蔡述雄,现腾讯用户体验设计部QQ空间高级UI工程师.智图图片优化系统首席工程师,曾参与<众妙之门> ...
- Spring MVC+ Spring + Mybatis从零开始搭建一个精美且实用的管理后台
点击进入<SSM搭建精美实用的管理系统>达人课页面 SSM 框架即 SpringMVC+Spring+Mybatis,相信各位朋友在投递简历时已直观感受到它的重要性,JavaWeb 相关工 ...
- A direct formulation for sparse PCA using semidefinite programming
目录 背景 Sparse eigenvectors(单个向量的稀疏化) 初始问题(low-rank的思想?) 等价问题 最小化\(\lambda\) 得到下列问题(易推) 再来一个等价问题 条件放松( ...
- 1171: lfx捧杯稳啦!
escription Lfx在复习离散的时候突然想到了一个算法题,毕竟是lfx, 算法题如下: 他想知道这样的问题,先定义1~n中即是3的倍数,又是11的倍数的那些数的和sum, 他想知道sum有多少 ...
- 07-nodejs中npm的使用
NPM是什么? 简单的说,npm就是JavaScript的包管理工具.类似Java语法中的maven,gradle,python中的pip. 安装 傻瓜式的安装. 第一步:打开https://node ...
- js根据ip自动获取地址(省市区)
HTML: <html> <head> <meta charset="utf-8"> <meta name="viewport& ...
- js 正则进阶regexp
一.匹配中文,英文字母和数字及_: const reg = /^[\u4e00-\u9fa5\w]+$/; const str1 = 'shangyy'; const str2 = '尚悦悦ww123 ...