Divisor Erasing Game 2
Time limit2sMemory limit512 MB
Given the board numbers, print for each possible first pick every reply that lets the second player force a win.
- Level
Medium7 of 10
- Topics
- Game theory, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
A and B play the divisor erasing game.
Several distinct natural numbers are written on a blackboard. On your turn you pick one number that is still on the board, erase it, and erase every divisor of it that is still on the board as well. For example, if the board holds 2, 3, 4, 5, 6 and you pick 6, then 6 goes away together with its divisors 2 and 3. You may not skip a turn. Whoever erases the last number wins.
A moves first, but the number A picks on the very first turn is chosen at random. So the winner, and the number B has to erase in order to win, both depend on which number A took first. Apart from that first move, A and B play optimally.
For each number A can erase first, find every number B can erase on the next turn and still win.
Input
The first line contains , the count of numbers on the board. ()
The second line contains the numbers on the board, separated by spaces and given in increasing order. The are distinct and satisfy .
Output
Print lines. Line starts with the -th input number wrapped in parentheses, so a line for starts with (6).
If B can win after A erases first, print after that, separated by single spaces, every number B can erase on the second turn to win, in increasing order. If B cannot win no matter what, print A instead of the numbers.
Hint
Take the board 1, 2, 3, 4, 5, 6. If A first picks 6, then 6 and its divisors 1, 2, 3 are erased and 4, 5 remain. Whichever of the two B erases, A erases the other one and wins, so B cannot win here. If A first picks 5, then 5 and 1 are erased and 2, 3, 4, 6 remain. Now B wins by answering 2 with 3, 3 with 2, 4 with 6, and 6 with 4.
Take the board 2, 3, 4, 6. If A erases 2, B erases 3, and if A erases 3, B erases 2. Either way 4 and 6 remain and B wins. If A erases 4, B erases 6, and if A erases 6, B erases 4, clearing the board and winning at once. In each case B loses if it erases anything else.