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

using namespace std;

unsigned int countSetBits(unsigned int number);
void showResult(std::vector<int>& resultVector);

int main()
{
    int expectedPower = 0;
    cin >> expectedPower;
    std::vector<int> resultVector;

    int currentNumber = 1;
    while (expectedPower >= 0)
    {
        auto powerOfCurrentNumber = countSetBits(currentNumber);
        expectedPower -= powerOfCurrentNumber;
        resultVector.push_back(currentNumber);
        currentNumber++;
    }


    if (expectedPower < 0)
    {
        currentNumber = resultVector.back();
        while (expectedPower != 0)
        {
            if (expectedPower + countSetBits(currentNumber) == 0)
            {
                resultVector.erase(resultVector.begin() + currentNumber - 1);
                break;
            }
            currentNumber--;
        }
    }

    showResult(resultVector);

    return 0;
}

unsigned int countSetBits(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}

void showResult(std::vector<int>& resultVector)
{
    cout << resultVector.size() << endl;
    //for (const auto number : resultVector)
    for (int i = resultVector.size() - 1; i >= 0; --i)
    {
        cout << resultVector[i] << " ";
    }
}