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

constexpr int N = 1e6 + 5;
int binsearch(vector<int> &v, int x)
{
    int l = 0, r = v.size() - 1;
    while (l < r)
    {
        int m = (l + r) / 2;
        if (v[m] < x)
            l = m + 1;
        else
            r = m;
    }
    return l;
}

int main()
{
    vector<int> pref(N, 0);
    int n;
    cin >> n;
    for (int i = 1; i < N; ++i)
    {
        bitset<21> b(i);
        pref[i] = pref[i - 1] + b.count();
    }
    int first = binsearch(pref, n);
    int dif = pref[first] - n;
    vector<int> res;
    while (first)
    {
        bitset<21> b(first);
        if (b.count() <= dif)
        {
            dif -= b.count();
            --first;
            continue;
        }
        res.emplace_back(first--);
    }
    cout << res.size() << endl;
    for (auto &x : res)
        cout << x << " ";
}