codeforces C. Bits 贪心
2016-05-18 23:19
369 查看
C. Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's denote as
![](http://codeforces.com/predownloaded/c0/97/c097ecd8556f4f8d61d8b143ef6afbc7928fce0b.png)
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
![](http://codeforces.com/predownloaded/c0/97/c097ecd8556f4f8d61d8b143ef6afbc7928fce0b.png)
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
output
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, 求l和r之间的数中二进制中1的个数最多的数是哪一个, 如果有多个答案, 输出最小的那个数.
分析: 我们可以贪心的一步一步的将l的最低位0 变成1, 因为要使1的个数最多, 直到l再取大于r的时候就不能取了,这时候l的二进制中1的数目最多, 并且是最小的.
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's denote as
![](http://codeforces.com/predownloaded/c0/97/c097ecd8556f4f8d61d8b143ef6afbc7928fce0b.png)
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
![](http://codeforces.com/predownloaded/c0/97/c097ecd8556f4f8d61d8b143ef6afbc7928fce0b.png)
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:
110 = 12
210 = 102
310 = 112
410 = 1002
510 = 1012
610 = 1102
710 = 1112
810 = 10002
910 = 10012
1010 = 10102
题意: 给定l和r, 求l和r之间的数中二进制中1的个数最多的数是哪一个, 如果有多个答案, 输出最小的那个数.
分析: 我们可以贪心的一步一步的将l的最低位0 变成1, 因为要使1的个数最多, 直到l再取大于r的时候就不能取了,这时候l的二进制中1的数目最多, 并且是最小的.
#include<bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N=100010,MOD=1e9+7; int main() { int n; cin>>n; while(n--) { ll l,r; cin>>l>>r; ll ans=l; while(l<=r) { ans = l; for(int i=0;i<61;i++){ if(!(l&(1LL<<i))){ l |= 1LL<<i; break; } } } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N=100010,MOD=1e9+7; int main() { int n; cin>>n; while(n--) { ll l,r; cin>>l>>r; ll ans=l; while(l<=r) { ans = l; ll m = (~l)&(-(~l)); l += m; } cout<<ans<<endl; } return 0; }
相关文章推荐
- 复杂链表的复制
- hdu 4284 状压dp
- avalon随笔
- Hibernate 脏数据检查机制与数据缓存
- 在listView中显示json数据出现的问题及解决方法
- maven基础篇(6)-maven仓库
- Android开发本地及网络Mp3音乐播放器(二十)歌曲下载完成后通知主界面更新本地音乐
- Android开发本地及网络Mp3音乐播放器(二十)歌曲下载完成后通知主界面更新本地音乐
- 前端开发案例--360首页左右滑动导航
- C/C++ memmove与memcpy的区别及实现
- 难道.NET Core到R2连中文编码都不支持吗?
- 小调网 经典电影 豆瓣评分 tampermonkey脚本
- 三、bash脚本参数的设定
- STACK 非标准版
- 面试题:用两个栈实现一个队列
- leetcode-5. Longest Palindromic Substring
- Openstack:一根中国稻草-【中国IC微专栏】2016.4.28
- Spark Streaming揭秘 Day11 Receiver Tracker的具体实现
- 第一讲算法(2)
- 修改文件.bash_profile后报错 /home/tomcat/.bash_profile: line 18: syntax error: unexpected end of file