uva 10718 - Bit Mask

19 篇文章 0 订阅

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


分析:

题目说明暴力法会超时。

采用位比较法。



代码:

#include 
   
   
    
    
#include 
    
    
     
     
using namespace std;
int main()
{
    unsigned n, l, u;
    while(cin >> n >> l >> u){
	bitset<32> bitn(n);
	bitset<32> bitl(l);
	bitset<32> bitu(u);
	bitset<32> bitm;
	int gtl = 0;
	int ltu = 0;
	for(int i = 31; i >= 0; i--){
	    if(bitu[i] == 1 && bitl[i] == 1){
		if(gtl == 0 || bitn[i] == 0)
		    bitm[i] = 1;
                else{
                    bitm[i] = 0;
                    ltu = 1;
                }
	    }else if(bitu[i] == 1 && bitl[i] == 0){
		if(bitn[i] == 0){
		    bitm[i] = 1;
		    if(gtl == 0)
			gtl = 1;
		}else{
		    bitm[i] = 0;
		    ltu = 1;
		}
	    }else if(bitu[i] == 0 && bitl[i] == 1){
		if(bitn[i] == 0){
		   if(ltu == 1) 
		       bitm[i] = 1;
		}else if(gtl == 0){
		    bitm[i] = 1;
		}
	    }else if(bitu[i] == 0 && bitl[i] == 0){
		if(bitn[i] == 0){
		    if(ltu == 1){
			bitm[i] = 1;
			if(gtl == 0)
			    gtl = 1;
		    }
		}
	    }
	}
	cout << bitm.to_ulong() << endl;
    }
    return 0;
}

    
    
   
   

别人的简洁代码:



  1. #include <cstdio>  
  2. #include <cmath>  
  3. #define repd(i, a, b) for (int i = (a); i >= (b); i--)  
  4.   
  5. const int MAXN = 100;  
  6.   
  7. int main() {  
  8.     long long n, l, u, m, t;  
  9.     while (scanf("%lld%lld%lld", &n, &l, &u) != EOF) {  
  10.         int len = ceil(log(u) / log(2));  
  11.         m = 0;  
  12.         repd(i, len, 1) {  
  13.             t = m | (1LL << (i - 1));         //1  
  14.             if (n >> (i - 1) & 1) {  
  15.                 if (t <= l) m = t;  
  16.             }  
  17.             else {  
  18.                 if (t <= u) m = t;  
  19.             }  
  20.         }  
  21.         printf("%lld\n", m);  
  22.     }  
  23.     return 0;  
  24. }  







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值