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
#include <iostream>
#include <bitset>
using namespace std;

int n;
int heads[1000001];
int tails[1000001];
int head_bits[1000001];
int final_sequence[1000001];
int current_head, bits;

int main(){
  cin >> n;
  current_head = 1;
  heads[1] = 1;
  tails[0] = 0;
  for(int i=2; i<=n; i++){
    bits = bitset<20>(current_head).count();
    if(heads[i - bits] >= current_head){
      current_head ++;
      bits = bitset<20>(current_head).count();
    }
    heads[i] = current_head;
    tails[i] = i - bits;
  }
  int j = n;
  int count = 0;
  while (j > 0){
    final_sequence[count] = heads[j];
    count ++;
    j = tails[j];
  }
  cout << count << endl;
  for(int i=0; i < count; i++){
    cout << final_sequence[i] << " ";
  }
  cout << endl;
}