Codeforces 1780 D: Bit Guessing Game

In this problem, we are given the number of unit bits in a hidden positive integer, and we must guess the integer within 30 moves. Each move, we can subtract an integer x from the hidden integer, and the number of unit bits in the new integer is given to us as a hint. The constraint is that 1≤n≤10^9.

One approach to solve this problem is to start by guessing the maximum value of n, which is 2^{cnt - 1}. We can do this by subtracting 2^{cnt - 1} from the hidden integer and check if the number of unit bits decreases by cnt. If it does, then our guess is correct, and we can print the result and move on to the next test case. If not, we can check if the number of unit bits decreases by cnt-1, cnt-2, and so on, until we find the correct value of n or we run out of moves.

Here is a sample implementation in Python:

t = int(input())
for _ in range(t):
    cnt = int(input())
    n = 0
    for i in range(30):
        n += 2**i
        if (bin(n).count("1") == cnt):
            print("!", n)
            break
        print("-", 2**i)
        cnt = int(input())

Explanation:

This approach works for the given constraints of the problem, since the maximum value of n is 2^{30}, which is less than 2^{31}, so we can represent it in an int data type.

Also, this approach is optimal because we are using the information given to us (number of unit bits) to narrow down the possibilities, and we are always subtracting the maximum possible value of n, which decreases the number of unit bits by cnt, cnt-1, cnt-2, and so on, which guarantees that we will find the correct value of n within 30 moves.