def f(x): p = 1 count = 0 while p << 1 <= x: p <<= 1 count += 1 bits = 0 while p: if p <= x: bits += count*(p >> 1) + x - p + 1 x -= p p >>= 1 count -= 1 return bits def ones(x): suma = 0 while x: suma += x & 1 x >>= 1 return suma def solve(n): first = 1 right = 1 while f(right) < n: right <<= 1 while first <= right: s = (first + right) >> 1 if f(s) < n: first = s + 1 else: right = s - 1 left = f(first) - n ans = [str(first)] last = first - 1 for i in range(first - 1, 0, -1): if ones(i) <= left: left -= ones(i) else: ans.append(str(i)) if not left: last = i - 1 break print(len(ans) + last) print(" ".join(ans + [str(i) for i in range(last, 0, -1)])) solve(int(input()))
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 | def f(x): p = 1 count = 0 while p << 1 <= x: p <<= 1 count += 1 bits = 0 while p: if p <= x: bits += count*(p >> 1) + x - p + 1 x -= p p >>= 1 count -= 1 return bits def ones(x): suma = 0 while x: suma += x & 1 x >>= 1 return suma def solve(n): first = 1 right = 1 while f(right) < n: right <<= 1 while first <= right: s = (first + right) >> 1 if f(s) < n: first = s + 1 else: right = s - 1 left = f(first) - n ans = [str(first)] last = first - 1 for i in range(first - 1, 0, -1): if ones(i) <= left: left -= ones(i) else: ans.append(str(i)) if not left: last = i - 1 break print(len(ans) + last) print(" ".join(ans + [str(i) for i in range(last, 0, -1)])) solve(int(input())) |