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
61
62
63
64
65
66
67
68
69
70
#include<cstdio>
#include<cmath>
#include<vector>


int g(int k, int i){
    return std::pow(2, k-1) * (k + 2 * i); 
}

int main(){
int n;
int res = 0;
int k = 20;
int i = 0;
std::vector<int> res_v;
scanf("%d", &n);
while(k > 0 && n > 0){
    if(g(k,i) <= n){
        res += std::pow(2, k);
        n -= g(k,i);
        i++;
    }
    k--;
}

if(n==0){
    res--;
}
else if (n <= i){
    n = i - n;
}
    
if(n > i){
    res++;
    n = 2* i + 1 - n;
}


std::vector<int> rem_v;
int rem = res;
i=-1;
while(rem > 0){
    if(rem % 2 == 0)
        rem_v.push_back(0);
    else
        rem_v.push_back(1);
    rem /= 2;
    i++;
}

while(n > 0){
    if(rem_v[i] == 1){
        rem += pow(2, i);
        n--;
    }
    i--;
}
    
if (rem != 0)
    printf("%d\n", res -1);
else
    printf("%d\n", res);
    
for(i=res; i>0; i--){
    if(i == rem)
        continue;
    printf("%d ", i);
} 
return 0;
}