Circle and Marble
Time limit1sMemory limit256 MB
Each move shifts one marble along an arrow to the next circle, and you decide if the first or second player wins under best play.
- Level
Medium7 of 10
- Topics
- Game theory, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
Adi and Putri have been together for years, and Adi is ready to propose. Putri does not want to make it easy. She asks Adi to play a game with her, and she will marry him if he wins.
Putri drew one circle and put some marbles inside it. Then she drew a second circle, put some marbles inside it, and drew one arrow from the first circle to the new one. Then she drew a third circle, put some marbles inside it, and drew one arrow from one of the circles already on the paper to the new one. She repeated this until she had drawn circles. Some circles may hold no marbles. No two circles overlap and no circle sits inside another.
The two players move alternately. On your turn you pick one circle, take exactly one marble out of it, and move that marble to one of the circles that the arrows leaving the chosen circle point to. Whoever cannot move loses.
A circle with no outgoing arrow cannot be picked. Moving the marble to another circle is mandatory, so a circle the marble cannot leave is not a legal choice. A circle holding no marble cannot be picked either. The same rules bind both players.
If both players play optimally, the starting configuration alone decides the winner. Putri lets Adi choose who moves first. Given the starting configuration, decide whether Adi should move first or second in order to win. Putri plays optimally, so she wins whenever she gets the chance.
Input
The first line contains the number of test cases ().
Each test case begins with a line containing , the number of circles Putri drew (). The circles are numbered to in the order she drew them.
The second line contains integers , where is the number of marbles in circle ().
The third line contains integers , where is the circle that the arrow pointing at circle starts from (). Circle was drawn first, so no arrow points at it and is always .
Output
For each test case, print one line in the form Case #X: Y. is the test case number starting from , followed by a single space. is first if Adi should take the first move to win, or second if he should move second.
Hint
These notations make the walkthrough shorter.
- move(a, b) means taking one marble out of circle and moving it to circle .
- lists the number of marbles in circle , circle , circle , and so on.
In the first case of the example input, the first player plays move(2, 3) and reaches . The second player has nothing but move(1, 2), reaching . The first player plays move(2, 3) again, reaches , and the second player cannot move.
In the second case, the first player loses whatever he plays. He has two moves.
- move(1, 2) reaches . The two players then take turns moving marbles from circle to circle , and the second player wins.
- move(2, 3) reaches . The second player answers with move(2, 3) to reach . The first player has nothing but move(1, 2), and the second player ends the game with move(2, 3).
So Adi should be the second player in this configuration.
In the third case, the first player plays move(1, 2) and reaches . The second player has nothing but move(3, 4), reaching . The first player moves the last marble of circle to circle , and the second player cannot move.