Nim/3
Time limit1sMemory limit128 MB
Three-player Nim where each player has a preferred winner; find player 1's optimal move with smallest stack then smallest count.
- Level
Medium7 of 10
- Topics
- Game theory, Dynamic programming, Brute force, Recursion
- Solved
- No attempts yet
Problem
You are staying in the country of Determinisia to take part in a challenging programming contest. The Determinisians are very fond of deterministic games — games whose result can be known in advance, assuming the players play optimally. They love watching everything unfold exactly as they had foreseen.
The great teacher Oneplusoneistwo invented the game of Nim thousands of years ago, and it is still very popular in Determinisia. The rules are simple: there are three (possibly empty) stacks of matches. Players take turns; on your turn you must choose a non-empty stack and remove any positive number of matches from it. The player who empties all three stacks — that is, who takes the last match — wins.
Recently the scientist Oneplustwoisthree proposed a three-player version of Nim, called Nim/3. To keep the game deterministic, each player designates one of the other two players as a favorite. If a player cannot win himself, he plays so that his favorite wins. Every player also knows everyone else's favorite. In short, each player's preference order is: (win yourself) > (your favorite wins) > (the remaining player wins).
You are player 1. To socialize with the local students, you want to play a few games of Nim/3, but only if you can play optimally. Fortunately, you are allowed to write a computer program to help you during play. Determine player 1's optimal move: which stack to draw from, and how many matches to take.
Input
The first line contains the number of test cases . Each test case is given in the following format.
- One line with six integers . Here are the number of matches on stacks 1, 2, and 3 respectively, with and . are the favorites of players 1, 2, and 3 respectively.
Note that . It is currently player 1's turn, followed by player 2, then player 3, and so on cyclically.
Output
For each test case, print player 1's move when he plays optimally, as two integers and on a single line separated by a space. Here is the stack and is the number of matches to take from that stack. There may be several optimal moves; in that case output the one with the smallest , and among those the smallest .