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
//Mateusz Kruk
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int>P(n+1,-1);
    P[0]=-99;
    P[1]=1;
    int il_bit=1;//ilosc bitow we wszystkich liczbach do tej pory
    int x=1;
    int y=1;
    for(int i=2;i<=n;i++){
        if(x>=i){
            P[i]=P[i-1];
        }
        else{
            y++;
            x+=__builtin_popcount(y);
            P[i]=y;
        }
    }
    vector<int>ans;
    int q=n;
    while(P[q]>0){
        if(q==1){
            ans.push_back(P[q]);
            q=0;
        }
        else{
            ans.push_back(P[q]);
            int k=q;
            q=k-__builtin_popcount(P[k]);
        }
    }
    cout<<ans.size()<<'\n';
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<" ";
    }
    cout<<'\n';
}