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
#include <iostream>
#include <vector>
#include <bitset>


class Node {
public:
    Node(): len(0), val(0), next(nullptr) {};
    Node(int val): len(1), val(val), next(nullptr) {};

    Node(Node * next, int val): next(next), val(val), len(1 + next->len) {};

    int len;
    int val;
    Node* next;
};

int main() {
    std::ios::sync_with_stdio(false);

    int n;
    std::vector<Node*> nodes;
    
    std::cin >> n;


    nodes.reserve(n+5);
    nodes[0] = new Node(1);

    int prevVal = 1;

    int missingBits, val;

    for (int i = 1; i < n; ++i) {
        val = prevVal;
        missingBits = i - std::bitset<32>(val).count();
        if (nodes[missingBits]->val >= val) {
            val++;
            missingBits = i - std::bitset<32>(val).count();
        }
        nodes[i] = new Node(nodes[missingBits], val);
        prevVal = val;
    }

    
    std::cout << nodes[n - 1]->len << "\n";

    auto it = nodes[n - 1];
    while (it != nullptr) {
        std::cout << it->val << " ";
        it = it->next;
    }
    std::cout << "\n";
    std::cout.flush();
    return 0;
}