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
#include <iostream>
using namespace std;

#include <iostream>
using namespace std;
int bitOnes[17] = {1, 3, 8, 20, 48, 112, 256, 576, 1280, 2816, 6144, 13312, 28672, 61440, 131072, 278528, 589824};
int sumOnes[17] = {1, 4, 12, 32, 80, 192, 448, 1024, 2304, 5120, 11264, 24576, 53248, 114688, 245760, 524288, 1114112};
int pow2[17] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
int *ones, n;

int main()
{
    cin >> n;
    int i=0, diff=0;
    while (sumOnes[i]<=n) i++;
    int firstAll = pow2[i]-1;
    int firsti = i, ti=0;
    diff = n-sumOnes[i-1];
    if (diff>0){
        i=0;
        while (bitOnes[i]<=diff) i++;
        firstAll += pow2[i-1];
        int secondi = pow2[i-1];
        diff -= bitOnes[i-1];
        if (diff >0 ){
            ones = new int[pow2[firsti]];
            ones[0] = 1;
            int j=0;
            while (j<firsti){
                for (int i=0; i<pow2[j]; i++){
                    ones[pow2[j]+i] = ones[i]+1;
                }
                j++;
            }
            j = secondi-1;
            while (diff>0){
                diff -= ones[++j];
            }
            firstAll += j-secondi+1;
            ti = j;
        }
    }
    if (diff==0){
        cout << firstAll <<endl;
        for (int i=firstAll; i>0; i--){
            cout << i << " ";
        }          
    }
    else{
        cout << firstAll-1 <<endl;
        int w=-diff;
        while ((ti>0) && (ones[ti] != w)){
            cout << firstAll-- << " ";
            ti--;
        }
        for (int i=firstAll-1; i>0; i--){
            cout << i << " ";
        }  
    }

    return 0;
}