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
53
54
55
56
57
58
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int countOnes(int n) {
    int count = 0;

    for (int i = 1; i <= n; i <<= 1)
        if (n & i)
            count++;

    return count;
}

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

    int n;
    cin >> n;

    // gen array
    vector<int> sums;
    int sum = 0;
    int i = 1;

    while (sum < n) {
        auto ones = countOnes(i);
        sums.push_back(i);
        sum += ones;
        i++;
    }

    // reduce array
    auto r = sum - n;
    vector<int> results;
    results.reserve(sums.size());

    for (auto rit = sums.rbegin(); rit != sums.rend(); ++rit) {
        auto num = *rit;
        auto ones = countOnes(num);
        if (r - ones >= 0) {
            r -= ones;
        } else {
            results.push_back(num);
        }
    }

    cout << results.size() << endl;
    for (auto num: results) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}