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
import numpy


def main():
    n = int(input())
    pow = 1
    while n > range_size(pow):
        pow += 1

    arr = []
    sums = []
    prev = 0
    num = 2 ** (pow - 1)
    while num < 2 ** pow:
        item = count_ones(num)
        arr.append(item)
        sums.append(item + prev)
        prev = sums[-1]
        num += 1

    rem = n - range_size(pow - 1)
    front = []
    while rem > 0:
        pos = numpy.searchsorted(sums, rem, side='left')
        front.append(pos + 2 ** (pow - 1))
        rem -= arr[pos]

    total = 2 ** (pow - 1) - 1 + len(front)
    print(total)
    print(' '.join(str(x) for x in front), end=' ')
    print(' '.join(str(x) for x in range(2 ** (pow - 1) - 1, 0, -1)))


def range_size(pow:int) -> int:
    return pow * 2 ** (pow - 1)


def count_ones(n: int) -> int:
    ones = 0
    while n > 0:
        if n % 2:
            ones += 1
        n //= 2
    return ones

if __name__ == "__main__":
    main()