CodeForces 484A Bits
来源:互联网 发布:win10软件消失 编辑:程序博客网 时间:2024/06/05 19:29
题意:
10000个询问 每个询问输入L和R(10^18) 输出在区间内二进制表示下1最多的数字 如果1个数相同输出最小的
思路:
YY一下 觉得后几位全是1的时候能保证1的个数多 那么如何构造出这个数字呢??
将L和R都变成二进制 从高位到低位 L和R相同的那几位一定是不变的 因为要保证构造出的数字在区间内 然后分两种情况
一是L和R一直相同 那就没什么好说的了 就是它了
二是发现了有一位不同 这时R的那个位一定是1 L的一定是0 那么只要把R的那个1变成0 然后把后面的所有位都变成1就构造出了数字 这时一定1最多吗?? 不一定 比如 R=101111 L=100000 构造出来是100111 这时要特判一下 如果把差异的那一位变回1是不是超过R 不超过的话 变回来更优
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef long long LL;int main() { int n; LL l, r, ans; scanf("%d", &n); while (n--) { cin >> l >> r; ans = 0; for (int i = 61; i >= 0; i--) { if ((r & (1LL << i)) && !(l & (1LL << i))) { ans |= (1LL << i); ans--; if (ans + (1LL << i) <= r) ans += (1LL << i); break; } else { if (r & (1LL << i)) ans |= (1LL << i); } } cout << ans << endl; } return 0;}
0 0
- codeforces 484a Bits
- codeforces 484 A-Bits
- CodeForces 484A Bits
- codeforces-484A Bits
- Codeforces 484A Bits
- codeforces 484A bits
- Codeforces-484A-Bits
- Bits - CodeForces 484 A 水题
- codeforces 484A Bits 数论
- Codeforces 484A Bits(贪心)
- Codeforces 484A - Bits (贪心)
- Codeforces 484A. Bits 贪心
- codeforces 484A Bits 贪心->位数
- [Codeforces 484A]Bits(拆位贪心)
- Review of codeforces 484A Bits based on python
- A. Bits(Codeforces Round #276(div1)
- Codeforces Round #276 (Div. 1) A. Bits
- Codeforces Round #276 (Div. 1) A. Bits
- oracle 定时任务
- 代码共享与可重入
- Unity3D研究院之IOS截屏 话筒录音 录制截屏视频保存沙盒
- Android 初识 Helloworld 详解
- Struts2如何获得request
- CodeForces 484A Bits
- Android Intent和PendingIntent的区别
- 优化sqlldr加载上100万条记录的数据!
- 怎样查SCI期刊的影响因子与排名
- 小白ACM-1的个数
- Ubuntu 12.04 LTS 64bit安装nodejs-express-npm-jade 我尝试失败不知道什么原因。
- 黑马程序员---面向对象
- requireJS在同一个HTML/JSP页面,加载不同版本的jquery
- java类集整理及例子