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
#include <iostream>

using namespace std;

int last[32];

int main() {
    
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    
    int size = 0;
    int value = 0;
    
    while (size < n) {
        
        value++;
        int popcnt = __builtin_popcount(value);
        size += popcnt;
        last[popcnt] = value;
    }
    
    int diff = size - n;
    cout << (value - (diff != 0)) << '\n';
    for (int i = value; i > 0; i--)
        if (i != last[diff])
            cout << i << ' ';
    
    return 0;
}