HDU 5491 the Next

Source: Internet
Author: User

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&LT;&LT;T)-1; -                  Break; -             } -         } -printf"Case #%d:%i64d\n", cs++, ret); -     } in     return 0; -}
View Code

HDU 5491 the Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.