Test instructions: Known as D (0<=d<2^31), S1, S2, where L is the number of the conversion of D to binary number 1, the title guarantee S1<=l<=s2, to find a number, meet the following conditions:
1, larger than D
2. The number of 1 in the conversion to binary is within [S1, S2]
3. Find the minimum number that satisfies the conditions of 1 and 2
Analysis:
1, first, the D plus 1, assuming that the number is X, to find the X conversion to binary 1 of the number of CNT.
2, if s1<=cnt<=s2, then output x
3, if cnt<s1, you should increase the number of 1, because to ensure that the number is found to be the smallest, so the binary number from the rightmost start to change.
Method: Right-to-left, will find the first 0 to 1, assuming that the number of bits found in the 0 is I (from right to left number the first number of digits is 0, and so on), that the number becomes 1 o'clock, D will increase 2^i. (Eg:5 binary Digit 101, the 1th bit of 0 into 1, then 111, converted to decimal is 7,,5 to 7 increased 2^1)
4, if cnt>s1, you should reduce the number of 1, because to ensure that the number is found to be the smallest, so the binary number from the rightmost start to change.
Method: Right-to-left, will find the first 1 to 0, assuming that the number of bits found in the 1 is I (from right to left number the first number of digits is 0, and so on), that the number becomes 0 o'clock, D will increase 2^i. (Eg:6 binary digit 110, the 1th bit of 1 into 0, then 1000, converted to decimal is 8,,6 to 8 increased 2^1)
5, cycle the above operation until the conditions are met.
6, the above solution for two reasons:
(1) because it is on the binary, so when cnt<s1 plus 1 o'clock, it is not necessary to call the Get function to find the number of binary 1 (if the problem is a violent water, it is easy to understand this sentence)
(2) The most important reason is that it skips a number of unsatisfied intermediate numbers.
As in the following example: D = 4, S1 = 1, s2 = 1, for ease of understanding you can see the table below.
According to the problem solving ideas, d+1 is 5, then because the second number of 1 is 2,cnt>s2, so according to the code operation D is 6, still cnt>s2, continue to operate by code, can get D for 8, skip the case of D 7, the larger the number of skipped phenomenon is more obvious, here no longer an example.
#include <cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<Set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#defineMin (A, B) a < b? A:b#defineMax (A, B) a < b? B:atypedefLong Longll;typedef unsignedLong LongLlu;Const intInt_inf =0x3f3f3f3f;Const intInt_m_inf =0x7f7f7f7f;Constll ll_inf =0x3f3f3f3f3f3f3f3f;Constll ll_m_inf =0x7f7f7f7f7f7f7f7f;Const intDr[] = {0,0, -1,1};Const intDc[] = {-1,1,0,0};Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMAXN = -+Ten;Const intMaxt =10000+Ten;using namespacestd;intA[MAXN];int Get(ll N) {//For the number of 1 in the binary number, the edge of the d+1 binary number into the array a, easy to find the first 1 or the first 0 from right to leftintK =0; intCNT =0; while(n) {a[k+ +] = n%2; if(N &1) ++CNT; N>>=1; } returnCNT;} ll POW (intk) {//Fast power, because the problem only requires 2 of the power of K, so only passed K ll ans= LL (1); ll x= LL (2); while(k) {if(K &1) Ans *=x; X*=x; K>>=1; } returnans;}intMain () {intT; scanf ("%d", &T); for(inti =1; I <= T; ++i) {memset (A,0,sizeofa); ll D; intS1, S2; scanf ("%lld%d%d", &d, &S1, &S2); printf ("Case #%d:", i); intCNT =Get(++D); while(1){ if(CNT >= s1 && CNT <=S2) {printf ("%lld\n", D); Break; } Else if(CNT <S1) { intK =0; while(A[k]) + +K; A[K]=1;//Change 0 to 1,a the binary number D of D is stored in the array+=POW (k); ++cnt;//Note the number of 1 to increase}Else if(CNT >S2) { intK =0; while(!a[k]) + +K; D+=POW (k);//This can be changed to Lowbit (D), the required function is CNT=Get(d) The number of//1 may be constant, possibly reduced, not only to recalculate the number of 1 in the binary of D, but also to update the A array, save the new D binary number, and then find the first 1 or the first 0} from right to left. } return 0;}
The function of Ps:lowbit is to find the binary number of 2^p,p X, the position of the first 1 from right to left (the first digit number is 0, and so on)
int lowbit (int x) {
Return x& (-X);
}
Eg: if x=9, then 9&-9 is 0000 1001 & 1111 0111, the result is 1. (Note that the binary is a complement, the leftmost bit is the sign bit, the positive number is 0, the negative number is 1, the original code becomes the complement is the bitwise negation plus 1)
While the binary number of 9 is 1001, the position of the first 1 from the right-to-left number is 0, so lowbit (9) = 2^0 = 1.
HDU 5491 the Next (bit operation)