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

int binSearch(vector<int> & a, int x){
    int lo = 0, hi = a.size(), mid;
    while(lo < hi){
        mid = (lo+hi)/2;
        if(x <= a[mid]) hi = mid;
        else lo = mid+1;
    }
    return lo;
}

int countSetBits(int n){
    unsigned int count = 0;
    while(n){
        n &= (n - 1);
        count++;
    }
    return count;
}

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

    int sum = 0, number = 1;
    map<int, int> numberForMaxSum, bitsForNumber;
    vector<int> sums;

    while(sum < n){
        bitsForNumber[number] = countSetBits(number);
        sum += bitsForNumber[number];
        numberForMaxSum[sum] = number;
        sums.push_back(sum);
        number++;
    }

    vector<int> result;
    int num;
    while(n > 0){
        num = numberForMaxSum[sums[binSearch(sums, n)]];
        result.push_back(num);
        n -= bitsForNumber[num];
    }

    cout << result.size() << "\n";
    for(int x : result){
        cout << x << " ";
    }
}