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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int mSize = 120207 + 5;

int tab[mSize];
vector<int> result;

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

    int n;
    int x;
    int sum;
    int lastEle;
    cin >> n;

    for (int i = 1; i < mSize; i++)
    {
        sum = 0;
        x = i;

        while (x)
        {
            sum += (x % 2);
            x >>= 1;
        }
        tab[i] = tab[i - 1] + sum;
    }

    for (int i = 1; i < mSize; i++)
    {
        if (tab[i] >= n)
        {
            n = n - tab[i] + tab[i - 1];
            result.push_back(i);
            lastEle = i;
            break;
        }
    }

    for (int i = lastEle; i > 0; i--)
    {
        if (tab[i] == n)
        {
            while (i)
            {
                result.push_back(i);
                --i;
            }
            break;
        }
        else if (tab[i] < n)
        {
            result.push_back(i + 1);
            n = n - tab[i + 1] + tab[i];
        }
    }

    cout << result.size() << '\n';

    for (auto z : result)
        cout << z << ' ';
    cout << '\n';

    return 0;
}