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

int total;
int sum;
int s;
std::vector<int> result;

int main(void)
{
    scanf("%d", &sum);
    for(s = 1; total < sum; ++s)
        total += __builtin_popcount(s);

    for(s -= 1; total > 0 && s > 0; --s)
    {
        int popcount = __builtin_popcount(s);
        total -= popcount;
        if(sum > total)
        {
            result.push_back(s);
            sum -= popcount;
        }
    }

    printf("%lu\n", result.size());
    for(int r: result)
        printf("%d ", r);

    puts("");
    return 0;
}