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

typedef long long int LL;

const int MAX_ELEM = 120250;

int n, sum, last_num;
bool elems[MAX_ELEM];

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n;

    for(int i = 1; i < MAX_ELEM; ++i)
    {
        sum += __builtin_popcount(i);

        if(sum >= n)
        {
            last_num = i;
            break;
        }
    }

    cout << (sum == n ? last_num : last_num-1) << endl;

    bool found = false;
    for(int i = last_num; i >= 1; --i)
    {
        if(!found && sum - __builtin_popcount(i) == n)
        {
            found = true;
            continue;
        }

        cout << i << ' ';
    }
    cout << endl;

    return 0;
}