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

using namespace std;

const int MAX = 1000006;
int n;
int ile[MAX], sum[MAX];
vector<int> res;

void prep()
{
    for (int i = 1; i <= n; i++)
    {
        ile[i] = __builtin_popcount(i);
        sum[i] = ile[i] + sum[i - 1];
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    prep();

    for (int i = n; i > 0; i--)
    {
        if (n <= sum[i] && n > sum[i - 1])
        {
            res.push_back(i);
            n -= ile[i];
        }
    }

    cout << res.size() << "\n";
    for (auto i : res)
        cout << i << " ";
}