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

int getBitCount(unsigned int u) {
    unsigned int uCount;
    uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);
    return ((uCount + (uCount >> 3)) & 030707070707) % 63;
}

int main() {
    int N;
    int sum = 0;
    int curr = 0;
    std::vector<int> result;

    scanf("%d", &N);
    while (sum <= N) {
        curr++;
        sum += getBitCount(curr);
    }

    for (int v=curr; v>=1; v--) {
        int k = getBitCount(v);
        if (sum-k>=N) {
            sum-=k;
        } else {
            result.push_back(v);
        }
    }

    printf("%d\n", (int)result.size());
    for (int i=0; i<result.size(); ++i) {
        printf("%d ", result[i]);
    }

    return 0;
}