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. mysql if对数据进行处理 having对数据进行查询 thinkphp中的exp支持更复杂的where查询

    很多时候,数据库获取的信息并不是我们最终想要的,需要通过if进行处理. where支持查询 having支持后查询(查询后的数据,再筛选) 代码如下: if ($this->_post('dos ...

  2. c语言枚举型常量

    #include <stdio.h> //代表百度工程师的级别 enum level { //如果没有指定初始的值 那么c语言会自动分配一个编号 整数编号 T14=,T13=,T12=,T ...

  3. [Cycle.js] Main function and effects functions

    We need to give structure to our application with logic and effects. This lessons shows how we can o ...

  4. 一个不错的PPT,扁平化设计,开放资源,要的进来

    开了那么多的博客,没做啥资源贡献,今天共享一个不错的PPT模板.例如以下图所看到的,须要的话留下邮箱 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFp ...

  5. .NET基础拾遗(4)委托和事件1

    一.委托初窥:一个拥有方法的对象 (1)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执行它所“持有”的方法. (2)如何使用委托? ①声明委托类型(delegate关键字) ...

  6. 动态绑定GridView数据源遇到问题

    1.GridView中的Button控件响应Command事件的时候出现System.ArgumentException: 回发或回调参数无效, 设置<pages enableEventVali ...

  7. PowerDesigner Mysql 主键自增、初始值、字符集

    自增 在你所要设为自增型的键上(比如你的id)双击,弹出一个Column Properties对话框,右下角有一个Identify的选择框,选中它OK,就可以了. 再去查看Preview,就能看到AU ...

  8. 关于DIPS的MVC 4.0项目发布与在IIS 7.0上的部署的方法

    本人技术笨拙,今天在发布DIPS的MVC4.0项目,并部署到IIS上,遇到各种问题.在查询相关资料后,最终得以解决,所以想把这个过程记录下来. 首先是MVC4.0项目的发布: 打开你的VS2012网站 ...

  9. Servlet编码和解码

    1.request.setCharacterencoding("XXX"); 前提是POST提交 在客户端编码对value的值进行编码之前,通知客户端用什么码表(XXX)编码 2. ...

  10. C++读取一串不知个数的数字

    #include <iostream> using namespace std; int main(){ ]; ; while(cin>>shuzu[i]){ i++; } ; ...