This topic I did not think about to see the problem, the reason is just because I think I can not think out, really no confidence ah ... Well, I really didn't drill into the subject.
Perseverance... Slowly cultivate, can not do so, can not easily see the puzzle ah ...
Analysis:
First of all, the topic has already said that the enumeration will definitely time out, so, what you see on the net is an enumeration of 32 bits, and then with N, to determine whether the current bit is 1 or 0,
If it is 0, it will be replaced by 1, unless the maximum value of the interval is greater than 1. Because one can guarantee the final or maximum result.
If it is 1, it will be replaced by 0 (this will ensure that the result is the same as the minimum number of selected) unless the maximum value of 0 is smaller than the interval minimum,
This means that all bits behind the current bit are 1 and smaller than the minimum value.
Think about it later.
Post code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm>using namespace std; int main () {Long long int m,t,temp,i,n,l,u;while (CIN >> n >> L >> U) {m = 0;for (i=31; i>=0; i--) {t = (L Ong long) 1 << i;//only think of 1, can be used to determine whether one is 1 or 0temp = (m|t);//Place the position at 1if ((n&t) ==0) {if (temp <= U)// If the bit is 0 and 1 is smaller than the interval maximum, small m = temp;} else {if (M+t-1 < L)//If the bit is 1 and the maximum reached after 0 is set to 1 less than the interval minimum, or 0m = temp;}} cout << m << Endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10718 bit Mask (bit operation greedy)