CodeForces 485C---Bits(或运算)

传送门:https://codeforces.com/problemset/problem/485/C
Description
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 l i, r i — the arguments for the corresponding query (0 ≤ l i ≤ r i ≤ 1018).

Output
For each query print the answer in a separate line.

Input
3
1 2
2 4
1 10

Output
1
3
7

思路:
主要运用到了一种方法,或运算。
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <map>

using namespace std;
const int inf=0x3f3f3f3f;
int n;
long long l,r;
int main()
{
	cin>>n;
	while(n--){
		cin>>l>>r;
		int index=1,temp=l,cnt=0;
		while((l|l+1)<=r) {
			l|=(l+1);
		}
		cout<<l<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值