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

using namespace std;

int numbers[120206];
int main() {
    int N;
    cin >> N;
    int countAll = 0;
    int last = 0;
    for (int i=1; i<125000 && countAll < N; i++) {
        int counter = 0;
        int num = i;
        while(num > 0) {
            if (num % 2 == 1) counter++;
            num /= 2;
        }
        countAll += counter;
        numbers[i] = counter;
        last = i;
        //cout << i << ": " << counter << ": " << countAll << endl;
    }
    //cout << "last: " << last << endl;
    int j = last;
    int more = countAll - N;
    cout << (j - ((more > 0) ? 1 : 0)) << endl;
    //cout << "more: " << more << endl;
    while (j > 0) {
        if (numbers[j] <= more) {
            //cout << "eliminated: " << j << "(" << numbers[j] << ")" << endl;
            more -= numbers[j];
            //if (more > 0) cout << "!!!!" << endl;
        } else {
            cout << j << " ";
        }
        j--;
    }

    cout << endl;
}