Paweł i Gaweł 2
Time limit1sMemory limit128 MB
Two players alternately remove stones from the leftmost or rightmost pile, and you decide who takes the last stone when both play their best.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming
- Solved
- No attempts yet
Problem
After settling who takes the upper floor of their new house, Paweł and Gaweł decide to play another game, this time for the right to use the attic.
They place piles in a row on the table, each holding at least stone. Stones within a pile are indistinguishable. Starting with Paweł, the players move alternately. On a move, a player chooses either the leftmost remaining pile or the rightmost remaining pile and removes any number of stones (at least ) from it.
The player who removes the last stone wins, and both players play optimally. Who earns the right to use the attic?
Input
The first line contains the number of test cases ().
Each test case spans two lines. The first line holds the number of piles , and the second line holds the stone counts of the piles from left to right, separated by spaces. (, )
Output
For each test case, print the answer on its own line: P if Paweł can force a win no matter how Gaweł plays, otherwise G.