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
51
52
#include <bits/stdc++.h>
using namespace std;

int bitsOf(int n) {
    int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int targetBits;
    cin >> targetBits;

    int i = 0;
    int bitsSum = 0;
    vector<int> numbers;
    vector<int> bits;
    while(true) {
        numbers.push_back(++i);
        int numberBits = bitsOf(i);
        bits.push_back(numberBits);
        bitsSum += numberBits;
        if (bitsSum >= targetBits) {
            break;
        }
    }

    vector<int> result;
    result.push_back(i);
    for (int j = numbers.size() - 2; j >= 0; j--) {
        if (bitsSum - bits[j] < targetBits) {
            result.push_back(numbers[j]);
        } else {
            bitsSum -= bits[j];
        }
    }

    cout << result.size() << endl;
    for (int j = 0; j < result.size(); j++) {
        cout << result[j];
        if (j < result.size() - 1) {
            cout << " ";
        }
    }
    return 0;
}