A. Bits
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
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:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102

题意:给定l和r,找一个数x,满足 l<=x<=r,且x的二进制形式中1的数量要最多

思路:模拟一下,从高位到地位扫,如果r[p]==l[p],那么答案的p位就和他们一样,否则必有r[p]=1 && l[p]=0,那就让答案p位为0,后面其余位为1

擦。。过了pretest没过final test,原因是我数组忘记初始化了,好粗心。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; bool a[N],b[N],c[N];
int mx; void ltob(ll x,bool *arr)
{
int sz=;
while(x)
arr[sz++]=x&,x>>=;
if(mx<sz) mx=sz;
} bool judge(int p)
{
for(;p>=;p--)
if(a[p]==) return ;
return ;
} void work(int p)
{
if(p<) return;
if(judge(p))
for(int i=p;i>=;i--) c[i]=;
else{
if(a[p] == b[p]) c[p]=a[p],work(p-);
else{
for(int i=p-;i>=;i--)
c[i]=;
}
}
} void run()
{
ll l,r;
scanf("%I64d%I64d",&l,&r);
if(l==r) cout<<l<<endl;
else{
mx=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
ltob(r,a);
ltob(l,b); // cout<<"a:";for(int i=mx-1;i>=0;i--) printf("%d",a[i]); puts("");
// cout<<"b:";for(int i=mx-1;i>=0;i--) printf("%d",b[i]); puts("");
memset(c,,sizeof(c));
work(mx-);
// cout<<"c:";for(int i=mx-1;i>=0;i--) printf("%d",c[i]); puts("");
ll ans = , base = ;
for(int i=;i<mx;i++){
if(c[i]) ans+=base;
base<<=;
}
cout<<ans<<endl;
}
} int main()
{
#ifdef LOCAL
// freopen("case.txt","r",stdin);
#endif
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}

CodeForces 484A Bits(水题)的更多相关文章

  1. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  2. CodeForces 705A Hulk (水题)

    题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...

  3. CodeForces 705B (训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...

  4. CodeForces 474B Worms (水题,二分)

    题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...

  5. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

  6. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  7. Codeforces 484A - Bits 二进制找1

    这题可以根据l, r 在二进制下的长度进行分类. l  的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为  (1LL << (r ...

  8. CodeForces 474A Keyboard (水题)

    题意:给定一个键盘,然后一行字母,和一个字符,代表把那一行字母在键盘上左移还是右移一位. 析:没什么好说的,直接暴力就好. 代码如下: #include<bits/stdc++.h> us ...

  9. CodeForces - 682B 题意水题

    CodeForces - 682B Input The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — ...

随机推荐

  1. 在ios开发中使用 try 和 catch 来捕获错误。

    本文转载至 http://blog.csdn.net/remote_roamer/article/details/7105776 抛出错误的代码 //如果返回的报文是错误信息,则抛出错误 if([ou ...

  2. N皇后问题算法

    N皇后问题的两种主要算法是试探回溯法和位运算法.前一种是经典算法,后一种是目前公认的最高效算法,后者比前者效率提高了至少一个数量级.很多问题可以借鉴位运算的思想. 以下是转载的我认为写的比较好的一篇N ...

  3. React-Native开源项目学习

    https://github.com/liuhongjun719/react-native-DaidaiHelperNew 借贷助手https://github.com/liuhongjun719/r ...

  4. JavaScript如何判断非空

    JavaScript判断非空的语句一般为: var elvis; if (typeof elvis !== "undefined" && elvis !== nul ...

  5. ubuntu16.04+cuda8.0+cudnn5.0+caffe

    ubuntu安装过程(硬盘安装)http://www.cnblogs.com/zhbzz2007/p/5493395.html“但是千万不要用麒麟版!!!比原版体验要差很多!!!”开关机的时候电脑最上 ...

  6. 我的Java开发学习之旅------>Java使用Fork/Join框架来并行执行任务

    现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多. 虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并未这 ...

  7. 我的Java开发学习之旅------>工具类:Java使用正则表达式分离出字符串中的中文和英文

    今天看到一个工具类使用正则表达式将一大段字符串中的中文和英文都分离出来了,在此记录一下,读者可以收藏! import java.util.ArrayList; import java.util.Col ...

  8. luoguP3769 [CH弱省胡策R2]TATT

    luoguP3769 [CH弱省胡策R2]TATT PS:做这题前先切掉 P4148简单题,对于本人这样的juruo更助于理解,当然dalao就当练练手吧 题目大意: 现在有n个四维空间中的点,请求出 ...

  9. 算法(Algorithms)第4版 练习 1.3.29

    代码实现: //1.3.29 package com.qiusongde.linkedlist; import java.util.Iterator; import java.util.NoSuchE ...

  10. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...