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 <iostream>
#include <vector>
#include <bitset>

using namespace std;

unsigned bit_cnt(unsigned num) {
    bitset<32> bs(num);
    return bs.count();
}

int main() {
    ios_base::sync_with_stdio(false);

    unsigned expected_sum;
    cin >> expected_sum;

    vector<unsigned> result;
    unsigned sum = 0;

    unsigned num = 1;
    while (sum < expected_sum) {
        sum += bit_cnt(num);
        result.push_back(num);
        num += 1;
    }

    unsigned i = result.size() - 1;
    while (sum != expected_sum) {
        if (sum - bit_cnt(result[i]) >= expected_sum) {
            sum -= bit_cnt(result[i]);
            result[i] = 0;
        }
        i -= 1;
    }

    unsigned cnt = 0;
    for (const auto &item: result) {
        if (item != 0) {
            cnt += 1;
        }
    }
    cout << cnt << endl;
    for (auto idx = result.rbegin(); idx != result.rend(); idx++) {
        if (*idx != 0) {
            cout << *idx << " ";
        }
    }
    cout << endl;

    return 0;
}