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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdio>
#include <vector>

using namespace std;

inline int CountBits(int v)
{
    int result = 0;
    while(v > 0)
    {
        result += (v & 1);
        v >>= 1;
    }
    return result;
}

int main()
{
    int n, i;
    vector<int> chain;
    int sum = 0;
    int removed = 0;
    
    scanf("%i", &n);
    
    i = 0;
    while(sum < n)
    {
        i++;
        sum += CountBits(i);
        chain.push_back(i);
    }
    
    while(sum > n)
    {
        i--;
        int bits = CountBits(i);
        if(sum - bits >= n)
        {
            removed++;
            sum -= bits;
            chain[i-1] = -1;
        }
    }
    
    printf("%i\n", (int)chain.size() - removed);
    
    bool first = true;
    for(i = chain.size() -1; i >= 0; i--)
    {
        if (chain[i] != -1)
        {
            if(first)
            {
                printf("%i", chain[i]);
                first = false;
            }
            else
            {
            printf(" %i", chain[i]);
            }
        }
    }

    printf("\n");
    
    return 0;
}