#include <iostream> using namespace std; int main() { int n; cin >> n; bool res[1000001]; int bin[1000001]; int s = 0; int i = 1; int res_size = 0; while(s < n) { int v = 0; int tmp = i; while(tmp > 0) { v += tmp % 2; tmp /= 2; } s += v; bin[i] = v; res[i] = 1; res_size++; i++; } i--; int max_i = i; while(s > n) { if(s - bin[i] >= n) { s -= bin[i]; res[i] = 0; res_size--; } i--; } cout << res_size << '\n'; for(int i = max_i; i > 0; i--) { if(res[i]) { cout << i << ' '; } } cout << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <iostream> using namespace std; int main() { int n; cin >> n; bool res[1000001]; int bin[1000001]; int s = 0; int i = 1; int res_size = 0; while(s < n) { int v = 0; int tmp = i; while(tmp > 0) { v += tmp % 2; tmp /= 2; } s += v; bin[i] = v; res[i] = 1; res_size++; i++; } i--; int max_i = i; while(s > n) { if(s - bin[i] >= n) { s -= bin[i]; res[i] = 0; res_size--; } i--; } cout << res_size << '\n'; for(int i = max_i; i > 0; i--) { if(res[i]) { cout << i << ' '; } } cout << '\n'; } |