Grid Game 3-angle
Time limit1sMemory limit2048 MB
Given stones on a triangular grid of up to 1e9 rows, decide whether the first player wins the game where each move removes up to K stones and adds up to K stones to cells below.
- Level
Hard9 of 10
- Topics
- Game theory, Math, Dynamic programming
- Solved
- No attempts yet
Problem
Your friends, Anda and Kamu, want to play a game called Grid Game and ask you to be the gamemaster. As the gamemaster, you set up a triangular grid of size . The grid has rows, numbered from to . Row has cells, and the th cell of row is denoted .

Before the game starts, different cells (numbered from to ) are chosen. At cell , you add stones. You then give Anda and Kamu an integer and start the game.
Anda and Kamu take turns, with Anda going first. On a turn, a player does the following.
- Chooses a cell with at least one stone on it.
- Removes at least one and at most stones from the chosen cell.
- For each cell with and , adds zero or more stones, but at most , to that cell.
The following illustrations show all the cells where stones can be added for . The left one chooses cell , and the right one chooses cell .

A player who cannot complete a turn because no stones remain on the grid loses, and the opposing player wins. Determine who wins if both players play optimally.
Input
The first line contains an integer (), the number of test cases.
Each test case starts with a line containing three integers , , and (; ). Each of the next lines contains three integers , , and (; ). The pairs are distinct.
The sum of over all test cases does not exceed 200000.
Output
For each test case, output one line with the player who wins if both play optimally. Output Anda if Anda, the first player, wins. Otherwise, output Kamu.