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
#include <iostream>

constexpr int MAX_N = 1000000;

bool deleted[MAX_N];
int stack[MAX_N];

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n;
	std::cin >> n;

	int stack_size = 0;
	for(int i = 1; n > 0; ++i)
		n -= __builtin_popcount(stack[stack_size++] = i);

	int num_deleted = 0;
	for(int i = stack_size-1; n < 0; --i)
		if(n+__builtin_popcount(stack[i]) <= 0)
			deleted[i] = true,
			++num_deleted,
			n += __builtin_popcount(stack[i]);
	
	std::cout << stack_size-num_deleted << '\n';
	for(int i = stack_size-1; i >= 0; --i)
		if(!deleted[i])
			std::cout << stack[i] << ' ';
	std::cout << '\n';

	return 0;
}