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
#include <bits/stdc++.h>

using namespace std;

set<pair<int, int>> prefSums;
vector<int> result;

int main() {
    int n;
    cin >> n;

    int tmpSum = 0;
    for (int i = 1; i <= n; ++i) {
        tmpSum += __builtin_popcount(i);
        prefSums.insert(make_pair(tmpSum, i));
        if (tmpSum > n) {
            break;
        }
    }

    while (n > 0) {
        auto it = prefSums.lower_bound({n, numeric_limits<int>::min()});
        int val = it->first, idx = it->second;

        result.push_back(idx);
        if (n == val) {
            for (int i = idx - 1; i >= 1; --i) {
                result.push_back(i);
            }
            break;
        } else {
            n -= __builtin_popcount(idx);

        }
    }

    cout << result.size() << endl;
    for (int i = 0; i < result.size(); ++i) {
        cout << result[i] << (i + 1 != result.size() ? " " : "");
    }
    return 0;
}