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
#include <cstdio>
#include <algorithm>

int main() {
    int n;
    scanf("%d\n", &n);

    int values[n], size = 0, removed = 0;
    int cnt = 0;

    while (n > 0) {
        n -= __builtin_popcount(++cnt);
        values[cnt - 1] = cnt;
    }

    int nCnt = cnt;
    while (n < 0) {
        if (n + __builtin_popcount(nCnt) <= 0) {
            n += __builtin_popcount(nCnt);
            values[nCnt - 1] = -1;
            removed += 1;
        }
        nCnt -= 1;
    }

    printf("%d\n", cnt - removed);
    for (int i=cnt - 1; i>=0; i--) {
        if (values[i] != -1) printf("%d ", values[i]);
    }
}