UVA 10718 Bit Mask
来源:互联网 发布:全球碳排放量数据 编辑:程序博客网 时间:2024/05/21 17:54
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, ifN is 100 and L = 50, U = 60 then M will be 59 and N OR M will be 127 which is maximum. If 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 N, L, U 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
因为涉及到位运算,所以先把n转化为二进制,然后根据或运算的性质来构造m
#include<iostream>using namespace std;int a[64];long long n, l, r, i, cnt;int main(){while (cin >> n >> l >> r){for (i = 0; i < 32; i++, n >>= 1) a[i] = n % 2;for (cnt = 0, i = 31; i >= 0; i--){cnt += (long long) 1 << i;if (!a[i] && cnt <= r || a[i] && cnt <= l) continue;//保证了值最大m最小且在范围内。cnt -= (long long) 1 << i;}cout << cnt << endl;}return 0;}
- UVa 10718 - Bit Mask
- UVa 10718 - Bit Mask
- uva 10718 - Bit Mask
- uva 10718 - Bit Mask
- UVA 10718 Bit Mask
- UVa 10718 - Bit Mask
- uva 10718 Bit Mask
- UVA - 10718 Bit Mask
- uva 10718 - Bit Mask
- UVA 10718 Bit Mask
- UVa 10718 - Bit Mask
- UVA - 10718 Bit Mask
- UVA 10718 Bit Mask
- uva 10718 Bit Mask
- UVA - 10718 Bit Mask 贪心
- UVA 10718 Bit Mask (快速幂 + 贪心)
- uva 10718 Bit Mask (位运算)
- uva 10718 Bit Mask(贪心)
- 请解释局部内部类和成员内部类都可以被static修饰吗,原因是什么?
- 请问实现多线程有几种方式?他们的区别是什么?
- 数组排序
- 请解释枚举和单例的区别?
- 01背包、完全背包、多重背包模版。
- UVA 10718 Bit Mask
- 1061
- uva 624 CD
- 指针与数组
- UVA 10382 Watering Grass
- 【反射】JAVA代理模式与动态代理
- file标签
- HDU 1171 Big Event in HDU
- UVA 10720 Graph Construction