Tutorial
To minimize the total number of moves, we should subtract the largest possible value, which is , as often as possible.
Let's divide by and analyze the remainder to find the optimal strategy. There are exactly 3 possible cases:
N \pmod 3 = 0: We can perfectly reduce using only s. We subtract exactly times. Total moves = .
N \pmod 3 = 2: We subtract as many times as possible ( times) and are left with exactly . We then subtract one time. Total moves = \lfloor N/3 \rfloor + 1.
N \pmod 3 = 1: If we greedily take out s, we are left with , which we cannot clear because we can only subtract or . Instead, we subtract one less time than the maximum, leaving a remainder of 3 + 1 = 4. We can then perfectly clear this by subtracting two times. Total moves = (\lfloor N/3 \rfloor - 1) + 2 = \lfloor N/3 \rfloor + 1.
Notice that for any that is not a perfectly divisible by , the minimum number of moves is always exactly \lfloor N/3 \rfloor + 1. We can compute and print this for each testcase.
Complexity Time Complexity: per testcase to compute the answer using basic arithmetic, resulting in overall. Space Complexity is .