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
73
74
75
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct twos {
    int num;
    int ones;
};

void bin(unsigned n, int &outs) {
    /* step 1 */
    if (n > 1)
        bin(n / 2, outs);

    /* step 2 */
    outs += n % 2;
}

int bin_ones(unsigned n) {
    int ones = 0;
    bin(n, ones);
    return ones;
}

int fill(twos a[], int n, int &sum) {
    int last = 0;
    for (int i = 0; sum <= n; ++i) {
        a[i].num = i + 1;
        a[i].ones = bin_ones(i + 1);
        sum += a[i].ones;
        last = i;
    }
    return last;
}

void adjust(twos a[], int diff, int last) {
    for (int i = last; i >= 0; --i) {
        if (a[i].ones == diff) {
            a[i].num = -1;
            return;
        }
    }
}

void print_tab(twos a[], int last) {
    for (int i = last; i >= 0; --i) {
        if (a[i].num > 0) {
            cout << a[i].num << ' ';
        }
    }
    cout << '\n';
}

int main() {
    int bits;
    cin >> bits;

    twos a[bits];
    int sum = 0;

    int last = fill(a, bits, sum);
    int diff = sum - bits;

    if (diff > 0) {
        adjust(a, diff, last);
        cout << last << '\n';
    } else {
        cout << last - 1 << '\n';
    }

    print_tab(a, last);

    return 0;
}