暴力枚舉 很少說php
#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\n", cas++, ans); } return 0; }