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

using namespace std;

int getSize(int l){
    int out = 0;
    while(l){
        if(l%2==1) out++;
        l/=2;
    }
    return out;
}

int main(){
    int n;
    cin>>n;

    int sum = 0;
    int i = 0;
    vector<int> nums;
    int maxi = 0;
    int tmp;
    vector<int> maxToSize = {0};
    int size = 0;
    while(sum<n){
        i++;
        size++;
        tmp = getSize(i);
        sum+=tmp;
        if(maxToSize.size()==tmp){
            maxToSize.push_back(i);
        }
        maxToSize[tmp] = i;
        maxi = i;
    }

    int toskip = -1;

    if(sum>n){
        toskip = maxToSize[sum-n];
        size--;
    }
    cout<<size<<endl;



    for(int i = maxi; i>=1; i--){
        if(i==toskip) continue;
        cout<<i<<" ";
    }


    return 0;
}