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
#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 1e6 + 17;
int bity[MAXN];
int dokiedy[MAXN];

void Preorder()
{
    for(int i = 1; i < MAXN; ++i)
    {
        int bit = 0;
        int j = i;
        while(j)
        {
            if(j % 2 == 1)
                ++bit;
            j /= 2;
        }
        bity[i] = bit;
        dokiedy[i] = dokiedy[i-1] + bit;
    }
}

int UperBound(int n, int p = 0, int k = MAXN)
{
    int s = (p+k)/2;
    if(p == k)
        return s;
    if(dokiedy[s] < n)
        return UperBound(n, s+1, k);
    if(dokiedy[s] > n)
        return UperBound(n, p, s);
    return s;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    Preorder();
    vector <int> odp;
    while(n)
    {
        int in = UperBound(n);
        odp.push_back(in);
        n -= bity[in];
    }
    int sise = odp.size();
    cout << sise << "\n";
    for(int i = 0; i < sise; ++i)
        cout << odp[i] << " ";
}