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
#pragma GCC optimize("Ofast")
#include <iostream>

#include <vector>

#include <climits>

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

	int sum = 1;
	unsigned long long a = 1;
	
	std::vector<int> counts;
	counts.push_back(1);
	int n; std::cin >> n;

	while(sum <= n)
	{
		a++;
		int temp = 0;
		for(unsigned long long i = 1; i <= LLONG_MAX; i <<= 1)
		{
			temp += static_cast<bool>(a & i);
		}
		sum += temp;

		counts.push_back(temp);
	}
	int element_count = counts.size();
	if(sum == n)
		goto fin;

	//remove from back as much as neccesary
	for(int i = element_count - 1; i >= 0; i--)
	{
		if(sum - counts[i] >= n)
		{
			sum -= counts[i];
			element_count--;
			counts[i] = -1;

			if(sum == n)
				break;
		}
	}

fin:
	std::cout << element_count << '\n';

	for(int i = counts.size() - 1; i >= 0; i--)
	{
		if(counts[i] != -1)
			std::cout << i + 1 << ' ';
	}
	std::cout << '\n';
}