The Next
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 305 Accepted Submission (s): 137
Problem Description
Let L denote the number of 1s in the integer D ' s binary representation. Given integers S1 and S2, we call D a wyh number if $S _1\leq l\leq s_2$.
With a given d, we would like to find the next wyh number Y, which is JUST larger than D. In other words, Y is the smallest wyh number among the numbers larger than D. Please write a program to solve this problem.
Input
The first line of input contains a number T indicating the number of test cases $ (T\leq 300000) $.
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed this $0\leq D < 2^{31}$ and D is a wyh number.
OutputFor each test case, the output a single line consisting the "Case #X: Y". X is the test Case number starting from 1. Y is the next wyh number.
Sample Input3 11 2 4 22 3 3 15 2 5
Sample OutputCase #1:12Case #2:25Case #3:17
SourceACM/ICPC Asia Regional Hefei online problem solving: direct greed
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4InlineintLowbit (intx) {5 returnx&-x;6 }7 intMain () {8 intKase,cs =1, S1,s2;9 LL D;Tenscanf"%d",&Kase); One while(kase--){ Ascanf"%i64d%d%d",&d,&s1,&S2); -LL ret = D +1, LB =Lowbit (ret); - intCNT =__builtin_popcount (ret); the while(CNT < S1 | | cnt >S2) { - intTMP = __builtin_popcount (LB-1); - if(S1 > cnt + tmp| | cnt >S2) { -RET + =lb; +LB =Lowbit (ret); -CNT =__builtin_popcount (ret); +}Else{ A intt = max (CNT,S1)-CNT; atRET + = (1<<T)-1; - Break; - } - } -printf"Case #%d:%i64d\n", cs++, ret); - } in return 0; -}
View Code
HDU 5491 the Next