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
#include <bits/stdc++.h>
using namespace std;

const int max_n = 17;
int n, a, c, j;
vector <int> v;
int last[max_n];

int main(){
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin>>n;
    while(n>0){
        if(a>n){
            c=last[a-n];
            c=j-c;
            cout<<j-1<<'\n';
            reverse(v.begin(), v.end());
            for(int i=0; i<j; i++){
                if(i==c)continue;
                if(v[i]==0) continue;
                else cout<<v[i]<<" ";
            }
            return 0;
        }
        if(a==n){
            cout<<j<<'\n';
            reverse(v.begin(), v.end());
            for(int i=0; i<j; i++){
                if(v[i]!=0) cout<<v[i]<<" ";
            }
            return 0;
        }
        if(a<n){
            j++;
            v.push_back(j);
            c=__builtin_popcount(j);
            a+=c;
            last[c]=max(last[c], j);
        }
    }
}