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
#include <iostream>
#include <bitset>
#include <limits>
#include <deque>
#include <algorithm>

// in range <1; 1'000'000>
int getInput() {
    int n;
    std::cin >> n;
    return n;
}

int countSetBits(const int value) {
    std::bitset<32> bset(value);
    return bset.count();
}

int main() {
    const int n = getInput();
    std::deque<int> set;
    int sum = 0;
    int i = 1;
    for (int i = 1; sum < n; i++) {
        sum += countSetBits(i);
        set.push_front(i);
    }
    const auto it = std::find_if(set.cbegin(), set.cend(), [sum, n](const int elem){
        return sum - countSetBits(elem) == n;
    });
    if (it != set.cend()) {
        set.erase(it);
    }
    std::cout << set.size() << "\n";
    for (int i = 0; i < set.size(); i++) {
        std::cout << set[i];
        if (i != set.size() - 1) {
            std::cout << " ";
        } 
    }
    std::cout << "\n";
}