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

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

    int n;
    cin >> n;

    int bits[n];
    bits[0] = 0;
    int s = 0;
    int i;
    for(i = 1; s < n; i++)
    {
        bits[i] = (i & 1) + bits[i / 2];
        s += bits[i];
    }
    i--;
    int b = 0;
    int w = 0;
    int m = i;
    int nn = i;
    if(s - n > 0)
    {
        for(int j = log(i) / log(2); b < s - n; j--)
        {
            int cp2 = pow(2, j);
            if(i > cp2) i -= cp2, w += cp2, b++;
        }
        nn--;
    }
    cout << nn << "\n";
    for(i = m; i >= 1; i--)
    {
        if(i != w) cout << i << " ";
    }
}