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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cmath>
using namespace std;

struct element {
    int number;
    int rest = 0;
    int size = 0;
};
element results[1000001];

int NumberOfBits(int number) {
    int mask = 1;
    int result = 0;
    for (int i = 0; i < 32; i++) {
        if ((number & mask) != 0)
            result++;
        mask = mask << 1;
    }
    return result;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int number;
    cin >> number;
    results[1] = { 1,0,1 };
    int current_begin = 2, n_bits_in_begin = 1;
    for (int i = 2; i <= number; i++) {
        if (results[i - n_bits_in_begin].number >= current_begin) {
            current_begin++;
            n_bits_in_begin = NumberOfBits(current_begin);
        }
        results[i] = { current_begin, i - n_bits_in_begin, results[i - n_bits_in_begin].size + 1 };
    }
    cout << results[number].size << '\n';
    while (number) {
        cout << results[number].number << ' ';
        number = results[number].rest;
    }
    cout << "\n";
}