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

using namespace std;

typedef long long ll;
const int MAX = 1e6 + 7;
ll prfx[MAX];

ll count(ll x)
{
	ll ret = 0;
	while (x > 0) {
		ret += (x & 1);
		x >>= 1;
	}

	return ret;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll n;
	cin >> n;

	// wylicz takie sumy prefiksowe jakby
	prfx[0] = 0;
	for (int i = 1; i <= n; i++)
		prfx[i] = prfx[i - 1] + count(i);

	// przejdz od gory do dolu i wywalaj najwieksze
	ll diff = prfx[n] - n;
	vector<int> ans;
	for (int i = n; i > 0; i--) {
		// mozna skasowac
		if (count(i) <= diff) {
			diff -= count(i);
			continue;
		}

		ans.push_back(i);
	}

	cout << ans.size() << '\n';
	for (int x : ans)
		cout << x << ' ';
	cout << '\n';

	return 0;
}