UVa 10718 bit Mask: greedy & bit operations

Source: Internet
Author: User
Tags time limit

10718-bit Mask

Time limit:3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 1659

In bit-wise expression, mask is a common term. You can get a certain bit-pattern using mask. For example, if your want to make the a 32-bit number zero, can use 0XFFFFFFF0 as mask and perform a bit-wi SE and operation. Here's have to find such a bit-mask.

Consider you are given a 32-bit unsigned integern. You are have to find a maskm such which L≤m≤u and N OR M is maximum. For example, IfN is Andl = x, U = Thenm'll be to be ANDN 127 OR M'll be which. If several value OfM satisfies the same criteria then you have to print the minimum value OfM.

Input
Each input starts with 3 unsigned integers N, L, and U where l≤u. Input is terminated by EOF.

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

Look, a brute force solution could 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

Greedy idea:

From high to low to consider,

If the first bit of n is 0, then M needs to be as much as 1 in this position, and after this bit becomes 1 m<=u;
If the first bit of n is 1, then M is required to be 0 in this position, but m cannot be too small to be m<l when L is in this position for 1 o'clock.

Complete code:

/*0.013s*/
     
#include <cstdio>  
typedef unsigned int UI;  
     
int main ()  
{  
    ui n, L, u, M, temp;  
    int i;  
    while (~SCANF ("%u%u%u", &n, &l, &u))  
    {  
        m = 0;  
        for (i =; I >= 0-i)  
        {  
            ///if n is 0, then M should try to be 1 in this bit, and after this one is 1 m<=u  
            ///if n is 1, then M should try to be 0 in this position, But m cannot be too small as L in this one bit for 1 o'clock m<l  
            ///Notice n<l this situation  
            temp = m | (1u << i); M + (1u << i) of///bitwise operation ((  
            n >> i) & 1) = = 0 && temp <= u | | (l >> i) & 1 && m < l) m = temp;  
        }  
        printf ("%u\n", m);  
    }  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.