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
#include <iostream>
#include <math.h>

using namespace std;

int bitsOn[1000000+2];

int main() {
    bitsOn[1] = 1;
    bitsOn[2] =1 ;
    for(int i=1;i<=1000000;i++) {
        if(i - (i & (-i)) == 0) {
            bitsOn[i] = 1;
            continue;
        }
        int highest = floor(log2(i));
        //cout<<"i: "<<i<<", hogh:" <<highest<<"\n";
        bitsOn[i] = 1 + bitsOn[i-(1<<highest-1)];
    }
    int n;
    cin>>n;
    int result = 0;
    int idx = 0;
    while(result < n) {
        result += bitsOn[idx+1];
        idx++;
    }

    if(result == n)
        cout<<idx;
    else 
        cout<<idx-1;
     cout<<"\n";
    int surplus = result-n;
    bool fixed = false;
    //cout<<"surplus: "<<surplus<<endl;
    for(int i=idx;i>=1;i--) {
        if(fixed == false && bitsOn[i] == surplus) {
            fixed = true;
            continue;
        }
        cout<<i<<" ";
            
    }

    return 0;
}