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

using namespace std;

int ones(int a);

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;	//1<=n<=1e6
	cin>>n;
	int l=1;
	for(; l*(1<<(l-1))<n;l++) ;
	int p = 1<<l;
	int s = (1<<(l-1)) * l - n;
	vector<bool> v;
	v.resize(p, true);
	int m = p-1;
	int z = p-1;
	while(s!=0)
	{
		for(; ones(m)>s; m--) ;
		s-=ones(m);
		v[m] = 0;
		m--;
		z--;
	}
	cout<<z<<"\n";
	while(--p)
	{
		if(v[p])
			cout<<p<<" ";
	}
	return 0;
}

int ones(int a)
{
	int res = 0;
	while(a!=0)
	{
		res += a & 1;
		a >>= 1;
	}
	return res;
}