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
#include <iostream>
constexpr int maxN = 1e6 + 100;
int pre[maxN];
int main()
{
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	int n;
	std::cin >> n;
	for (int i = 1; i <= n; i <<= 1)
		for (int j = i; j <= n; ++j)
			pre[j] += ((j & i) != 0);
	int k = 1;
	int presum = pre[0];
	for (int i = 1; i <= n; ++i)
	{
		presum += pre[i];
		if (presum >= n)
		{
			k = i;
			break;
		}
	}
	int n2 = presum - n;
	std::cout << k - (n2 > 0) << "\n";
	for (int i = k; i > 0; --i)
	{
		if (pre[i] == n2)
			n2 = maxN;
		else
			std::cout << i << " ";
	}
}