Problem Description
Let L denote the number of 1s in integer D’s binary representation. Given two integers S1 and S2, we call D a WYH number if S1≤L≤S2.
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.
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≤300000).
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that 0≤D<231 and D is a WYH number.
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that 0≤D<231 and D is a WYH number.
Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the next WYH number.
Sample Input
3
11 2 4
22 3 3
15 2 5
Sample Output
Case #1: 12
Case #2: 25
Case #3: 17
Source
暴力枚举 不多说
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int oo = 1e9; const double PI = acos(-1); const int N = 1e3; int binary[N], one, k; void get(LL n) { one = k = 0; while(n) { if(n % 2 == 1) { binary[k] = 1; one++; } else binary[k] = 0; k++; n /= 2; } k--; } int main() { int T, s1, s2, L, cas=1; LL ans; scanf("%d", &T); while(T--) { memset(binary, 0, sizeof(binary)); scanf("%d %d %d", &L, &s1, &s2); ans = L; do { ans ++; get(ans); if(one < s1) { int j = s1 - one, id=0; while(j) { if(binary[id] == 0) { binary[id] = 1; j--; } id++; } break; } }while(one < s1 || one > s2); ans = 0; for(int i = k; i >= 0; i--) ans = ans * 2 + binary[i]; printf("Case #%d: %lld ", cas++, ans); } return 0; }