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

int bits(int k) {
    int res = 0;
    do {
        res += k & 1;
    } while (k >>= 1);
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, sum = 0, curr = 0;
    cin >> n;
    vector<int> highestPerBitNum;
    highestPerBitNum.resize(22);
    while (sum < n) {
        int bitCount = bits(++curr);
        sum += bitCount;
        highestPerBitNum[bitCount] = curr;
    }
    int diff = sum - n;
    cout << (diff ? curr - 1 : curr) << endl;
    while (curr) {
        if (curr != highestPerBitNum[diff]) cout << curr << ' ';
        --curr;
    }
}