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 <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int n;

int main(){
  fastio;
  cin >> n;
  
  int sum = 0;
  vector<int> res;
  vector<int> g(20);
  int us = 0;

  for(int i = 1; i <= n; i++){
    int p = __builtin_popcount(i);
    if(sum + p <= n){
      sum += p;
      g[p] = i;
      res.push_back(i);
      if(sum == n) break;
    } else{
      int nad = sum + p - n;
      sum -= nad;
      us = g[nad];
      i--;
    }
  }
  
  reverse(res.begin(), res.end());
  cout << res.size() - (us!=0) << "\n";
  for(auto r : res) if(r != us) cout << r << " ";
}