problem Description
inch if is are this problem.
Input
The first line of input contains a number T indicating the number of test cases (t≤300000case
as
is
0≤d<
231
is a wyh number.
Output
Case is Case from 1 is the next wyh number.
Sample Input
3 One 2 4 A 3 3 the 2 5
Sample Output
Case #1 Case #2 Case #3
Source
ACM/ICPC Asia Regional Hefei Online
Don't want to write, find someone else's.
Ideas:
Consider the minimum number of corrections by comparing the current number of 1.
First the D plus 1, and then calculate the number of D 1 tot, this time to compare the size of tot and s1,s2, this time there are two situations:
1, TOT<S1, then I need to increase the number of 1, because to the smallest, so I start from the right to find a 0 (position is assumed to be i), D plus 2^i.
2, TOT>S2, then I need to reduce the number of 1, so I start from the right to find a 1, will D plus 2^i.
So loop the above process until the tot complies with the S1,S2 condition.
The reason for the above operation is: I add 2^i, that is, the one from 1 to 0 or 0 to 1, and I started from the right to look for, can guarantee that every time the changes are minimal. At the same time my d is always increasing, so when the condition is met, it is the smallest one.
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - intdirx[]={0,0,-1,1}; - intdiry[]={-1,1,0,0}; - #definePI ACOs (-1.0) + #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long A #defineEPS 1e-10 at #defineMOD 1000000007 - #defineN 100 - #defineINF 1e12 - ll D,s1,s2; - ll Num[n]; - ll K; in ll Count_ (ll dd) { -memset (NUM,0,sizeof(num)); toll cnt=0; +k=0; - while(DD) { the if(dd&1) cnt++; *Num[k++]= (dd&1); $dd>>=1;Panax Notoginseng } -num[k++]=0; the + returnCNT; A } the intMain () + { -ll ac=0; $ ll T; $scanf"%i64d",&t); - while(t--){ -scanf"%i64d%i64d%i64d",&d,&s1,&S2); the -ll dd=d+1;Wuyi the while(1){ -ll tmp=Count_ (DD); Wu - if(tmp<S1) { About for(LL i=0; i<k;i++){ $ if(num[i]==0){ -dd+= (1<<i); - Break; - } A } +}Else if(tmp>S2) { the for(LL i=0; i<k;i++){ - if(num[i]==1){ $dd+= (1<<i); the Break; the } the } the}Else{ - Break; in } the } theprintf"Case #%i64d:",++AC); Aboutprintf"%i64d\n", DD); the the } the return 0; +}
View Code
Hdu 5491 The Next (violent enumeration)