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

#define MAX_N 1000000

using namespace std;

int n;
int maxBitCounts[MAX_N];
vector<int> solve;

void prepareMaxBitCounts() {
    maxBitCounts[0] = 0;
    for (int i = 1; i < MAX_N; i++) {
        maxBitCounts[i] = maxBitCounts[i - 1] + __builtin_popcount(i);
    }
}

int main() {
    prepareMaxBitCounts();

    scanf("%d", &n);

    int bitsLeft = n;
    int nextNumber = MAX_N;

    while(bitsLeft > 0) {
        while (nextNumber > 0 && maxBitCounts[nextNumber - 1] >= bitsLeft) {
            nextNumber--;
        }
        solve.push_back(nextNumber);
        bitsLeft -= __builtin_popcount(nextNumber);
        nextNumber--;
    }
    printf("%lu\n", solve.size());
    for (size_t i = 0; i < solve.size(); i++) {
        printf("%d%c", solve[i], (i == solve.size() - 1) ? '\n' : ' ');
    }
}