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. ANALYZE - 收集与数据库有关的统计

    SYNOPSIS ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ] DESCRIPTION 描述 ANALYZE 收集有关 PostgreSQL ...

  2. 删数问题(Noip1994)

    1321:[例6.3]删数问题(Noip1994) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 5127     通过数: 1595 [题目描述] 输入一个高精度 ...

  3. android ListView几个比较特别的属性

    由于这两天在做listView的东西,所以整理出来一些我个人认为比较特别的属性,通过设置这样的属性可以做出更加美观的列表 首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你 ...

  4. P2P实现的原理

    为了项目的后期IM应用,最近在研究libjingle,中间看了也收集了很多资料,感慨网上很多资料要么太过于纠结协议(如STUN.ICE等)实现细节,要么中间有很多纰漏.最后去伪存真,归纳总结了一下,希 ...

  5. ERC20 Token

    pragma solidity ^0.4.8; contract Token{ // token总量,默认会为public变量生成一个getter函数接口,名称为totalSupply(). uint ...

  6. 关于ie8下disabled属性:字体颜色问题

    在ie8下,input/textarea输入框如果使用disabled属性,字体的颜色会变灰,这时我们可以使用另一种方法实现它. 不使用disabled,用readonly代替: input[read ...

  7. ruby on rails安装(win7x64)

    Ruby下载地址http://rubyinstaller.org/downloads/ (以安装2.1.7为例,2.2.3未能安装成功) 安装完之后测试是否安装成功

  8. mysql数据库导出导入

    MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...

  9. Memcache 分布式存储 【一致性Hash】crc32

    class memcacheHash { private $_node = array(); private $_nodeData = array(); private $_keyNode = 0; ...

  10. MySQL-----改

    改 **修改用户名** rename user 'username'@'IP address' to 'new username'@'IP address'; **修改密码** set passwor ...