Increasing Subsequences (Codeforces 1922E)
Tags: Greedy, divide and conquer

Let's recall that an increasing subsequence of the array $$$a$$$ is a sequence that can be obtained from it by removing some elements without changing the order of the remaining elements, and the remaining elements are strictly increasing (i. e $$$a_{b_1} < a_{b_2} < \dots < a_{b_k}$$$ and $$$b_1 < b_2 < \dots < b_k$$$). Note that an empty subsequence is also increasing.You are given a positive integer $$$X$$$. Your task is to find an array of integers of length at most $$$200$$$, such that it has exactly $$$X$$$ increasing subsequences, or report that there is no such array. If there are several answers, you can print any of them.If two subsequences consist of the same elements, but correspond to different positions in the array, they are considered different (for example, the array $$$[2, 2]$$$ has two different subsequences equal to $$$[2]$$$).

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.The only line of each test case contains a single integer $$$X$$$ ($$$2 \le X \le 10^{18}$$$).

Output: For each query, print the answer to it. If it is impossible to find the required array, print -1 on the first line. Otherwise, print a positive integer $$$n$$$ on the first line — the length of the array. On the second line, print $$$n$$$ integers — the required array itself. If there are several answers, you can print any of them. All elements of the array should be in the range $$$[-10^9; 10^9]$$$.


Sample Inputs:
4
2
5
13
37

Sample Outputs:
1
0
3
0 1 0
5
2 2 3 4 2
7
-1 -1 0 0 2 3 -1

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    x = 200
    ans = []
    while n > 1:
        if n & 1 == 1:
            ans.append(-1)
            n -= 1
        else:
            ans.append(x)
            x -= 1
            n //= 2
    ans = list(reversed(ans))
    print(len(ans))
    print(' '.join(map(str, ans)))

Solution (Python):
cases = int(input())
for _ in range(cases):
    n = int(input())
    res = []
    hi = 100
    lo = -100
    while n != 2:
        if n & 1:
            res.append(lo)
            n -= 1
            lo += 1
        else:
            res.append(hi)
            hi -= 1
            n //= 2
    res.append(0)
    res = res[::-1]
    print(len(res))
    print(' '.join(map(str, res)))

Solution (Python):
t = int(input())
for _ in range(t):
    x = int(input())
    cur = 1000000
    neg = -1
    rem = x
    sz = 65
    st = False
    if x == 2:
        print('1\n0')
        continue
    answer = []
    for i in range(sz - 1, -1, -1):
        mask = 1 << i
        bit = (mask & rem) >> i
        if bit and (not st):
            st = True
            continue
        if st and bit:
            answer.extend([cur, neg])
            cur += 1
            neg -= 1
        if st and (not bit):
            answer.append(cur)
            cur += 1
    print(len(answer))
    print(' '.join(map(str, answer)))