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
#include <iostream>
#include <set>
int n;
using namespace std;
int tab[25];
int main() {
    cin >> n;

    int counter = 0;
    int last_n = 0;
    for (int i = 1; i <= n; i++) {
        int count2 = 0;
        int num = i;
        while (num > 0) {
            if (num % 2 == 1) {
                count2++;
            }
            num /= 2;
        }
        tab[count2] = i;
        counter += count2;
        if (counter >= n) {
            last_n = i;
            break;
        }
    }
    cout << last_n - ((counter == n) ? 0 : 1) << endl;
    for (int i = last_n; i >= 1; i--) {
        if (counter > n && tab[counter - n] == i) {
            continue;
        } else {
            cout << i << " ";
        }
    }
    cout << endl;
    return 0;
}