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
#include <iostream>
#include <vector>

using namespace std;

int oneCount(int x) {
    int res = 0;
    while(x > 0) {
        res += x&1;
        x>>=1;
    }
    return res;
}

void minSong(int n) {
    vector<int> arr;
    int i = 1;
    while((i+1) * (1<<i) <= n) {
        ++i;
    }
    int sum = i * (1<<(i - 1));
    int x = 1<<i;
    while(sum < n) {
        arr.push_back(x);
        sum += oneCount(x);
        x++;
    }
    int c = 0;
    for(auto iter = arr.rbegin() + 1; iter != arr.rend(); ++iter) {
        if(sum - n >= oneCount(*iter)) {
            sum -= oneCount(*iter);
            *iter = 0;
            c++;
        }
    }
    int total = (1<<i) - 1 + arr.size() - c;
    cout << total << endl;
    for(auto iter = arr.crbegin(); iter != arr.crend(); ++iter) {
        if(*iter != 0)
            cout << *iter << " ";
    }
    for (int j = (1<<i) - 1; j > 0; --j) {
        cout << j << " ";
    }
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    minSong(n);
    return 0;
}