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
#include <bits/stdc++.h>
#include <stdlib.h>

using namespace std;

//__builtin_popcount(n)

pair<int,bool> dopelnienie(int val, int n){
    int v = val;
    int p = 1;
    while(v && n>0){
        if(v%2 == 0){
            val+=p;
            n--;
        }
        p*=2;
        v/=2;
    }
    return {val, n==0};
}

vector<int> find_seq(int n){
    vector<int> res;

    if(n<=4){
        if(n == 1)return{1};
        else if(n == 2)return{1,2};
        else if(n == 3)return{1,3};
        else if(n == 4)return{1,2,3};
    }
    
    int ind = 1;
    int pop_cnt = __builtin_popcount(ind);
    
    while(n>0){
        res.push_back(ind);
        n-=pop_cnt;
        ind++;
        pop_cnt = __builtin_popcount(ind);
    }
    
    ind = res.size()-1;
    while(n<0){
        pop_cnt = __builtin_popcount(res[ind]);
        if(pop_cnt+n <= 0){
            n += pop_cnt;
            res[ind] = 0;
        }
        ind--;
    }
    vector<int> end_res;
    for(int r:res)if(r)end_res.push_back(r);
    return end_res;
}

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

    vector<int> tab = find_seq(n);
    cout<<tab.size()<<"\n";
    for(int i = tab.size()-1; i>=0; i--)cout<<tab[i]<<" ";cout<<"\n";


    return 0;
}