eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Doubloon Game

Doubloon Game

\includegraphics{https://static.e-olymp.com/content/df/dfd01a5895b1e6b7c8776cffd50f0b881f1a1b72.jpg} Being a pirate means spending a lot of time at sea. Sometimes, when there is not much wind, days can pass by without any activity. To pass the time between chores, pirates like to play games with coins. An old favorite of the pirates is a game for two players featuring one stack of coins. In turn, each player takes a number of coins from the stack. The number of coins that a player takes must be a power of a given integer \textbf{K} (\textbf{1}, \textbf{K}, \textbf{K^2}, etcetera). The winner is the player to take the last coin(s). Can you help the pirates gure out how the player to move can win in a given game situation? \InputFile The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format: \begin{itemize} \item One line with two integers \textbf{S} and \textbf{K}, satisfying \textbf{1} ≤ \textbf{S} ≤ \textbf{10^9} and \textbf{1} ≤ \textbf{K} ≤ \textbf{100}: the size of the stack and the parameter \textbf{K}, respectively. \end{itemize} \OutputFile For every test case in the input, the output should contain one integer on a single line: the smallest number of coins that the player to move can take in order to secure the win. If there is no winning move, the output should be \textbf{0}.
Ліміт часу 1 секунда
Ліміт використання пам'яті 32 MiB
Вхідні дані #1
5
5 1
3 2
8 2
50 3
100 10
Вихідні дані #1
1
0
2
0
1