uva 10718  Bit Mask  (位运算)

Problem A

Bit Mask

Time Limit

1 Second

In bit-wise expression, mask is a common term. You can get a certain bit-pattern using mask. For example, if you want to make first 4 bits of a 32-bit number zero, you can use 0xFFFFFFF0 as mask and perform a bit-wise AND operation. Here you have to find such a bit-mask.

Consider you are given a 32-bit unsigned integer N. You have to find a mask M such that L ≤ M ≤ U and N OR M is maximum. For example, if is 100 and L = 50, U = 60 then M will be 59 and N OR M will be 127 which is maximumIf several value of M satisfies the same criteria then you have to print the minimum value of M.

Input
Each input starts with 3 unsigned integers NLU where L ≤ U. Input is terminated by EOF.

Output
For each input, print in a line the minimum value of M, which makes N OR M maximum.

Look, a brute force solution may not end within the time limit.

Sample Input

Output for Sample Input

100 50 60
100 50 50
100 0 100
1 0 100
15 1 15

59
50
27
100
1


Problem setter: Md. Kamruzzaman
Member of Elite Problemsetters' Panel


这题很简单,从最高位开始逐位判断,如果N的该位为0,为使M | N的值最大,M的该位应考虑置为1,然后判断M的该位为1时的可能取值区间[lmin, lmax],如果区间[lmin, lmax]与区间[L, U]有交集,则说明该位可置为1;如果N的该位为1,为使M的值尽可能小,M的该位应考虑置为0,然后判断可能取值区间与[L, U]是否有交集,如果没有交集,该位置为1。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; long long n,l,r,a[100],b[100];
int cnta,cntb; void get(){
long long x=n;
cnta=0;cntb=0;
while(x>0){
a[cnta++]=x%2;
x/=2;
}
x=r;
while(x>0){
b[cntb++]=x%2;
x/=2;
}
} void computing(){
long long ans=0;
if(cntb>cnta){
for(int i=cntb-1;i>cnta-1;i--){
long long tmp=(long long)1<<(long long)i;//此处不强转,会超出int范围,wa了几次
if(b[i]==1){
l-=tmp;
r-=tmp;
ans+=tmp;
}
}
}
for(int i=cnta-1;i>=0;i--){
long long tmp=(long long)1<<(long long)i;
if(tmp<=l){
l-=tmp;
r-=tmp;
ans+=tmp;
}
else if(a[i]==0 && tmp<=r){
l-=tmp;
r-=tmp;
ans+=tmp;
}
}
printf("%lld\n",ans);
} int main(){
while(scanf("%lld%lld%lld",&n,&l,&r)!=EOF){
get();
computing();
}
return 0;
}
AC不了,请尝试以下测试数据:

INPUT:
22 1 21
17 3 5
12 9 17
622 435 516
774 887 905
398 119 981
550 99 427
823 684 966
22398 14719 27341
29787 21141 30633
3113 24242 29497
5021 11052 19571
7359 6669 19790
993331 367623 921769
810986 227838 492138
987964 208251 1034287
1002648 217556 236589
680047 402532 767548
27719912 10747535 22001285
16862072 13339946 28844391
20421198 11724734 31928748
1541522 10226990 20764468
22651935 2478699 31095674
1995360670 908222743 1868651617
305542918 594331857 1426036714
1265639777 1580376576 1885248297
1442823820 658800174 1919310996
604563406 1050668699 2128532112
1 0 4294967295

OUTPUT:
9
4
17
435
905
625
409
712
14721
23460
29462
19554
17216
382924
237589
257219
234343
499600
14223127
16692359
13133233
19429997
10902496
957429345
1305069817
1880088222
704659827
1542920241
4294967294

uva 10718 Bit Mask (位运算)的更多相关文章

  1. UVA 10718 Bit Mask 贪心+位运算

    题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...

  2. uva 10718 Bit Mask(贪心)

    题目连接:10718 Bit Mask 题目大意:给出一个T, 和一个下限L, 上限R, 在[L, R]之间找一个数, 使得这个数与T做或运算之后的数值最大 输出这个数. 解题思路:将T转换成二进制, ...

  3. UVa 10718 - Bit Mask

    题目大意:给一数N,在区间[L, U]上找到一个数M使得M| N的值最大,如果有M有多个可能值,取最小的那个值. 从最高位开始逐位判断,如果N的该位为0,为使M | N的值最大,M的该位应考虑置为1, ...

  4. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

  5. UVA - 13022 Sheldon Numbers(位运算)

    UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...

  6. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

  7. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  8. UVA 565 565 Pizza Anyone? (深搜 +位运算)

      Pizza Anyone?  You are responsible for ordering a large pizza for you and your friends. Each of th ...

  9. UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

    题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...

随机推荐

  1. @@ROWCOUNT 含义

    返回受上一语句影响的行数. 如果行数大于 20 亿,请使用 ROWCOUNT_BIG. Transact-SQL 语句可以通过下列方式设置 @@ROWCOUNT 的值: 将 @@ROWCOUNT 设置 ...

  2. <php>添加数据注意事项

    如果报错信息里有:fetch_all(),肯定是sql语句写错 get传值:<a href="chuli.php?name=1&code=2">处理</a ...

  3. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之全然具体解释

      HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth究竟指的哪到哪的距离之全然具体解释scrollHeight: 获取对象的滚动高度. scrol ...

  4. oracle 格式化数字 to_char

    转:http://blog.csdn.net/chinarenzhou/article/details/5748965 Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,i ...

  5. gcc/g++/make 编译信息带颜色输出

    假设编译一个项目错误警告太多.很不好找,所以很希望输出信息能够带有颜色. 但是 gcc 4.9.0 之前的版本号并不支持,非常多情况下是不能替换编译器的,比方使用交叉编译器, 也能够使用 colorg ...

  6. java.lang.OutOfMemoryError: Java heap space错误及处理办法

      以下是从网上找到的关于堆空间溢出的错误解决办法: java.lang.OutOfMemoryError: Java heap space ============================= ...

  7. 多路复用I/O epoll()

    epoll 是Linux内核中的一种可扩展IO事件处理机制,最早在 Linux 2.5.44内核中引入,可被用于代替POSIX select 和 poll 系统调用,并且在具有大量应用程序请求时能够获 ...

  8. Oracle SQL ANY和ALL语句

    Oracle的嵌套子查询可以使用Some,Any和All对子查询中返回的多行结果进行处理. Some表示满足其中一个的含义,是用or串起来的比较从句. 例如:SELECT * FROM emp WHE ...

  9. 搭建LAMP架构

    1. 为什么下载源码包需要到官网上去下载?简单说就是为了安全,如果是非官方下载的源码包,有可能被别有用心的人动过手脚. 2. 64位机器是否可以安装32位rpm包?64位机器是否可以安装32位的mys ...

  10. (转载)Linux网络配置和setup工具包安装

    查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...