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
56
57
58
59
60
61
62
63
#include <iostream>
#include <bitset>
#include <algorithm>

using namespace std;

const int MaxNum = 120206;

int numBits[MaxNum + 5] {0};
int sumBits[MaxNum + 5] {0};

int main() {
    int bitsCnt;

    ios::sync_with_stdio(false);

    for (int i=1; i<=MaxNum; ++i) {
        bitset<64> bn(i);
        numBits[i] = bn.count();
        sumBits[i] = numBits[i] + sumBits[i-1];
    }

    cin >> bitsCnt;

    int pos = 1;
    int *pp = lower_bound(sumBits, sumBits + MaxNum, bitsCnt);

    int over = (*pp) - bitsCnt; 
    int len = pp - sumBits;

    int removed = MaxNum;

    if (over) {
        removed = len -1;
        
        while (numBits[removed] != over) {
            --removed;
        }
    }

    if (removed < MaxNum) {
        cout << len-1 << endl;

        for (int val = len; val > removed; --val) {
            cout << val << " ";
        }

        for (int val = removed-1; val>0; --val) {
            cout << val << " ";
        }
    }
    else {
        cout << len << endl;

        for (int val = (pp-sumBits); val>0; --val) {
            cout << val << " ";
        }
    }
    
    cout << endl;

    return 0;
}