def bit_count(n): count = 0 while n > 0: count += 1 n = n & (n-1) return count def main(): SUM = int(input()) bit_counts = [] numbers = [] count = 0 number = 1 while count < SUM: bit_counts.append(bit_count(number)) numbers.append(number) count += bit_counts[-1] number += 1 i = len(bit_counts) - 2 while count > SUM: if count - bit_counts[i] >= SUM: count -= bit_counts[i] numbers.pop(i) # bit_counts.pop(i) i -= 1 numbers.reverse() print(len(numbers)) print(*numbers) if __name__ == '__main__': main()
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 | def bit_count(n): count = 0 while n > 0: count += 1 n = n & (n-1) return count def main(): SUM = int(input()) bit_counts = [] numbers = [] count = 0 number = 1 while count < SUM: bit_counts.append(bit_count(number)) numbers.append(number) count += bit_counts[-1] number += 1 i = len(bit_counts) - 2 while count > SUM: if count - bit_counts[i] >= SUM: count -= bit_counts[i] numbers.pop(i) # bit_counts.pop(i) i -= 1 numbers.reverse() print(len(numbers)) print(*numbers) if __name__ == '__main__': main() |