http://acm.hdu.edu.cn/showproblem.php?pid=5491
< Span id= "Mathjax-element-4-frame" class= "Mathjax" >
main topic: Given a number D, It has a binary number of 1 is the number of L, the minimum value of the number of the larger than D x and X of the binary number of 1 num satisfies s1 <= num <= s2
Analysis:
change d+1 to N to find the number of 1 in the second binary number L
&NBSP;
(1) If L is in the range of (S1, S2), direct output
(2) If the num<s1, from right to left 0 of the smallest bit I, the bit 0 to 1 (that is: 1 of the number is less, add a 1), the value of n increases 2^i (i.e.: n + = 2^i)
(3) If the NUM>S2, from right to left to find 1 of the smallest bit I, the bit +1 (that is: 1 of the number of more, need to reduce 1), the value of n increases 2^i (i.e.: n + = 2^i)
left-to-right to ensure minimum n value increase
#include <stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>Const intN = the; typedefLong Longll;using namespacestd;intD[n];int Get(ll N) {intj =0, num =0; while(n) {d[j+ +] = n%2; if(n%2==1) Num++; N/=2; } returnnum;}//find the number of 1 in the binary of nll Pow (intAintb) {ll ans=1; while(b) {if(b%2==1) ans*=A; A*=A; b/=2; } returnans;}//on the power of a in the power of fast numberintMain () {intT, num, a, b, x =0; ll N; scanf ("%d", &t); while(t--) {x++; scanf ("%i64d%d%d", &n, &a, &b); memset (d,0,sizeof(d)); N++; Num=Get(n); printf ("Case #%d:", x); while(1) { if(Num >= a && num <=b) {printf ("%i64d\n", N); Break; } Else if(Num <a) {intK =0; while(D[k]) K++; D[K]=1; N+ = Pow (2, K); Num++; } Else if(Num >b) {intK =0; while(!D[k]) k++; N+ = Pow (2, K); Num=Get(n); } } } return 0;}
Hdu 5491 the Next (bit operation)