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
76
//
//  main.cpp
//  2022-2-muz-muzyka-pop-2
//
//  Created by Marek on 14/12/2022.
//

#include <iostream>
#include <vector>
#include <algorithm>

constexpr int NBITS = 21;
constexpr int MAXN = 1 << NBITS;
//constexpr int BUFF = 1000;
//constexpr int BUFFS = BUFF * 30;
constexpr int INF = 1e7;

int number_of_bits[MAXN];
int number_of_ones[MAXN];

int smallest_begin[MAXN];

int lowest_next[MAXN][NBITS];

int main(int argc, const char * argv[]) {
    int k;
    for (int i = 0; i < MAXN; i++) {
        for (int j = number_of_bits[i]; j < NBITS; j++) {
            k = i | (1 << j);
            number_of_bits[k] = j + 1;
            number_of_ones[k] = number_of_ones[i] + 1;
        }
    }
    
    for (int i = MAXN - 2; i >= 0; i--) {
        for (int j = 0; j < NBITS; j++) {
            lowest_next[i][j] = lowest_next[i + 1][j];
        }
        lowest_next[i][number_of_ones[i + 1]] = i + 1;
    }
    
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    
    int n, sm = 0, cnt1, hmleft, cnt2, i2;
    std::cin >> n;
    
    for (int i = 1; i < MAXN; i++) {
        smallest_begin[i] = INF;
    }
    
    int l;
    for (int i = 0; i <= n; i++) {
        l = smallest_begin[i];
        for (int j = 0; j < NBITS; j++) {
            if (lowest_next[l][j]) {
                smallest_begin[i + j] = std::min(smallest_begin[i + j], lowest_next[l][j]);
            }
        }
    }
    
    std::vector<int> result;
    while (n > 0) {
        result.push_back(smallest_begin[n]);
        n -= number_of_ones[smallest_begin[n]];
    }
    
    std::cout << result.size() << '\n';
    for (int r : result) {
        std::cout << r << ' ';
    }
    std::cout << '\n';
    
    return 0;
}