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
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e6;
int n;
int count_bits(int x) {
    int count = 0;
    while (x) {
        count += x & 1;
        x >>= 1;
    }
    return count;
}
int cnt[NMAX + 7];
int sum, ans;
stringstream ss;
int32_t main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    cin >> n;
    int j = 1;
    while (sum < n) {
        sum += cnt[j++] = count_bits(j);
    }
    for (int x = j; x >= 1; x--) {
        if (sum - cnt[x] >= n) {
            sum -= cnt[x];
        } else {
            ss << x << " ";
            ans++;
        }
    }
    cout << ans << "\n" << ss.str();
    return 0;
}
// DAWID PAWELEC