CF BUDDY
← Problems·

1661B · Getting Zero

1300 · bitmasks, brute force, dfs and similar

Problem: Suppose you have an integer vv. In one operation, you can:

  • either set v=(v+1)mod32768v = (v + 1) \bmod 32768
  • or set v=(2v)mod32768v = (2 \cdot v) \bmod 32768.

You are given nn integers a1,a2,,ana_1, a_2, \dots, a_n. What is the minimum number of operations you need to make each aia_i equal to 00?

Input Format: The first line contains the single integer nn (1n327681 \le n \le 32768) — the number of integers.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai<327680 \le a_i < 32768).

Output Format: Print nn integers. The ii-th integer should be equal to the minimum number of operations required to make aia_i equal to 00.

Note: Let's consider each aia_i:

  • a1=19a_1 = 19. You can, firstly, increase it by one to get 2020 and then multiply it by two 1313 times. You'll get 00 in 1+13=141 + 13 = 14 steps.
  • a2=32764a_2 = 32764. You can increase it by one 44 times: 32764327653276632767032764 \rightarrow 32765 \rightarrow 32766 \rightarrow 32767 \rightarrow 0.
  • a3=10240a_3 = 10240. You can multiply it by two 44 times: 1024020480819216384010240 \rightarrow 20480 \rightarrow 8192 \rightarrow 16384 \rightarrow 0.
  • a4=49a_4 = 49. You can multiply it by two 1515 times.

Sample Cases

Case 1

Input

4
19 32764 10240 49

Output

14 4 4 15

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback