// Mateusz Bogusławski #include <iostream> using namespace std; inline int count(int n) { int c = 0; while (n) { c += n & 1; n >>= 1; } return c; } int main() { int n, c = 0, counter = 0; cin >> n; bool f[200000] = {}; int max = 1; for (int i = 1; c < n; i++) { c += count(i); counter++; f[i] = true; max = i; } for (int i = max; c != n; i--) { if (c-count(i) >= n) { c -= count(i); f[i] = false; counter--; } } cout << counter << endl; for (int i = max; counter > 0; i--) { if (f[i]) { cout << i << " "; counter--; } } cout << endl; }
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 | // Mateusz Bogusławski #include <iostream> using namespace std; inline int count(int n) { int c = 0; while (n) { c += n & 1; n >>= 1; } return c; } int main() { int n, c = 0, counter = 0; cin >> n; bool f[200000] = {}; int max = 1; for (int i = 1; c < n; i++) { c += count(i); counter++; f[i] = true; max = i; } for (int i = max; c != n; i--) { if (c-count(i) >= n) { c -= count(i); f[i] = false; counter--; } } cout << counter << endl; for (int i = max; counter > 0; i--) { if (f[i]) { cout << i << " "; counter--; } } cout << endl; } |