CodeForces484A Bits(贪心)

CodeForces484A

题目大意:给出范围【A。B】。期望你给出某个数X满足X属于【A,B】,而且X转成二进制的1的个数最多。假设有多个给出最小的数。

解题思路:由于须要1最多,那么我们先将每一个位都放上1,假设这个数减掉了某一位的1还是超出了范围,那么就能够去掉这个1。假设去掉后发现比A小了,那么这个位置上的1是不能去掉的。直到找到一个数在范围内即为所求的。每次去掉的是高位的1那么在保证1的个数同样的情况下求得的自然是最小的数。

代码:

#include <cstdio>

const int maxn = 63;
typedef long long ll; int N;
ll a, b;
ll base[maxn];
ll judge(ll tmp, int pos, int cnt); void init () {
base[0] = 1L;
for (int i = 1; i < maxn; i++) {
base[i] = base[i - 1] * 2L;
}
} ll handle () { ll ans = base[maxn - 1] - 1;
for (int i = maxn - 2; i >= 0; i--) {
if (ans - base[i] > b)
ans -= base[i];
else if (ans - base[i] >= a && ans - base[i] <= b)
return ans - base[i];
}
} int main () { scanf ("%d", &N);
init();
while (N--) {
scanf ("%lld%lld", &a, &b);
printf ("%lld\n", handle());
}
return 0;
}

CodeForces484A Bits(贪心)的更多相关文章

  1. CodeForces484A——Bits(贪心算法)

    Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...

  2. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  3. codeforces 484A A. Bits(贪心)

    题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #276 (Div. 1) A. Bits 贪心

    A. Bits   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non ...

  5. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  6. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  7. UVa 12545 Bits Equalizer (贪心)

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作. 析:先说我的思路,我看到这应该是贪心,首先,如果先判断s能不能变成t, ...

  8. UVa 12545 Bits Equalizer【贪心】

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作 先可以画个草图 发现需要考虑的就是 1---0 0---1 ?---0 ...

  9. UVA - 12545 Bits Equalizer (比特变换器)(贪心)

    题意:输入两个等长(长度不超过100)的串S和T,其中S包含字符0,1,?,但T只包含0和1,你的任务是用尽量少的步数把S变成T.有以下3种操作: 1.把S中的0变成1. 2.把S中的“?”变成0或1 ...

随机推荐

  1. 5-Java-C(单位分数)

    题目描述: 形如:1/a 的分数称为单位分数. 可以把1分解为若干个互不相同的单位分数之和. 例如: 1 = 1/2 + 1/3 + 1/9 + 1/18 1 = 1/2 + 1/3 + 1/10 + ...

  2. sqlalchemy ORM进阶- 批量插入数据

    参考: https://www.jb51.net/article/49789.htm https://blog.csdn.net/littlely_ll/article/details/8270687 ...

  3. mathAge.call(btn) 函数call 改变函数内 this #js

    mathAge.call(btn) 函数call 改变函数内 this

  4. dom监听事件class

    layui.use(['layer', 'form'], function(){ var layer = layui.layer ,form = layui.form; var $ = layui.j ...

  5. CAD参数绘制半径标注(网页版)

    主要用到函数说明: _DMxDrawX::DrawDimRadial 绘制一个半径标注.详细说明如下: 参数 说明 DOUBLE dCenterX 被标注的曲线的中点X值 DOUBLE dCenter ...

  6. 打印出A到Z的所有字符,使用char和int转换

    public class Demo14{ //A到Z的所有字符,使用char和int转换 public static void main(String[] args) { for(int i = 65 ...

  7. 他山之石:D3DX书籍推荐

    一.DirectX9.0.3D游戏开发编程基础(龙书) 这本书网上评论很好,当初作为入门书看确实还不错,但是看得有点稀里糊涂的.现在回过头来看,它有些地方省略了,所以还得自己多多总结,总得来说还是值得 ...

  8. 关于nested exception is org.apache.ibatis.binding.BindingException:Parameter '***' not found报错解决

    几天晚上遇到的奇怪的问题  传入的参数名一直没有变   但是从mapper到xml似乎有一个找不到参数的报错,实际上只要在Mapper接口形参前加“@Param(“形参名称”)”就可以了

  9. 看完这篇 你就能完全操作git 远程分支的增、删、改、查了

    最近项目中又用到了git所以在此总结一番,这篇主要针对的是怎么创建远程分支,如何删除远程分支. 首先,如何创建远程分支.将一系列前期准备工作准备完成后(创建\添加ssh): 在终端键入 git bra ...

  10. 升级openssh踩得坑

    升级背景: 项目中使用的系统为CentOS6.8,经过漏洞扫描后发现openssh高危漏洞,具体描述如下:OpenSSH 7.2p2之前版本, sshd/ session.c/ do_setup_en ...