N = int(input()) def get_bit_count(value): n = 0 while value: n += 1 value &= value-1 return n i = 1 l_bits_cnt = [] result = 0 while result != N: cnt_bit = get_bit_count(i) if result + cnt_bit <= N: result += cnt_bit l_bits_cnt.append((cnt_bit, i)) else: elements_to_remove_cnt = 0 elements_to_remove_sum = 0 idx = len(l_bits_cnt) - 1 while idx >= 0: last_bits_cnt, last_number = l_bits_cnt[idx] elements_to_remove_cnt += 1 elements_to_remove_sum += last_bits_cnt tmp_result = result + cnt_bit - elements_to_remove_sum idx -= 1 if tmp_result > N: continue elif tmp_result == N: while elements_to_remove_cnt > 0: l_bits_cnt.pop() elements_to_remove_cnt -= 1 l_bits_cnt.append((cnt_bit, i)) result += cnt_bit result -= elements_to_remove_sum break elif tmp_result < N: break i += 1 nrs = [str(nr) for (cnt_bit, nr) in l_bits_cnt] print(len(nrs)) nrs = nrs[::-1] for i in range(len(nrs)-1): print(nrs[i], end=' ') print(nrs[-1])
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 | N = int(input()) def get_bit_count(value): n = 0 while value: n += 1 value &= value-1 return n i = 1 l_bits_cnt = [] result = 0 while result != N: cnt_bit = get_bit_count(i) if result + cnt_bit <= N: result += cnt_bit l_bits_cnt.append((cnt_bit, i)) else: elements_to_remove_cnt = 0 elements_to_remove_sum = 0 idx = len(l_bits_cnt) - 1 while idx >= 0: last_bits_cnt, last_number = l_bits_cnt[idx] elements_to_remove_cnt += 1 elements_to_remove_sum += last_bits_cnt tmp_result = result + cnt_bit - elements_to_remove_sum idx -= 1 if tmp_result > N: continue elif tmp_result == N: while elements_to_remove_cnt > 0: l_bits_cnt.pop() elements_to_remove_cnt -= 1 l_bits_cnt.append((cnt_bit, i)) result += cnt_bit result -= elements_to_remove_sum break elif tmp_result < N: break i += 1 nrs = [str(nr) for (cnt_bit, nr) in l_bits_cnt] print(len(nrs)) nrs = nrs[::-1] for i in range(len(nrs)-1): print(nrs[i], end=' ') print(nrs[-1]) |