Editorial for COCI '10 Contest 4 #6 Hrpa
Submitting an official solution before solving the problem yourself is a bannable offence.
The idea of solving this kind of problem consists of creating a brute force solution for small input instances () and observing solution patterns for solving big cases.
Solution for small uses a dynamic programming approach. Let denote if a player can win regardless of its opponent's actions (winning position) if there are pebbles remaining, and he can take at most rocks in the current turn. These are the possible transitions:
under the assumption that there are enough rocks (), otherwise it is allowed to take rocks at most. If every neighbouring state is a losing position, we can conclude that our current state is a winning position. From these observations, we can quickly create an algorithm.
Observing the outputs for small we can notice that solutions we are looking at are in fact Fibonacci numbers. We can also observe that most of the solutions are not too big (except for the Fibonacci numbers and numbers which can be written as sums of Fibonacci numbers - their solutions are big). From these facts our intuition tells us that we might be able to find a solution if we try to disassemble the number as a (unique) sum of non-consecutive Fibonacci numbers - the solution is then in fact the smallest number in the sum. That algorithm can be implemented in complexity.
For those who want to learn more about this type of problem - try to look for the math game called "Fibonacci Nim".
Comments