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
// Wojciech Widomski
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    
    int n;
    cin >> n;
    vector<int> ciag;
    int liczba = 1;
    int ostatnie_bity = 0;

    while (n>0) {
        int bity = bitset<32>(liczba).count();

        ciag.push_back(liczba);

        n -= bity;
        liczba++;
    }

    int zly_element = -1;

    if (n < 0) {
        for (int i = ciag.size() -1; i >= 0; i--) {
            if (bitset<32>(ciag[i]).count() == -n) {
                zly_element = i;
                break;
            }
        }
    }

    if (zly_element != -1) {
        cout << ciag.size() -1 << "\n";
    } else {
        cout << ciag.size() << "\n";
    }

    for (int i = ciag.size() - 1; i >= 0; i--) {
        if (i != zly_element) {
            cout << ciag[i] << " ";
        }
    }

    cout << "\n";

    return 0;
}