Mancala
Time limit1sMemory limit256 MB
For each marble count N, print the pits of the unique Tchoukaillon board that sweeps every marble into the Roumba.
- Level
Medium7 of 10
- Topics
- Math, Simulation, Game theory
- Solved
- No attempts yet
Problem
Mancala is a family of board games with many rule variants. This problem uses the simplest one, the one player game Tchoukaillon.
Tchoukaillon is played on a board whose pits sit in a single row. The leftmost pit is the Roumba, and the Roumba starts empty. The pits to the right of the Roumba are numbered 1, 2, 3, ... from left to right, and is the number of marbles in pit .

One turn goes in this order.
- Find a pit with . If several pits qualify, you can pick any of them.
- Empty that pit and hand out its marbles one at a time to the Roumba and to pits 1 through .
If marbles remain on the board and no pit satisfies , the position is lost. The first row of the figure above shows pit 3 being chosen.
If repeated turns move every marble into the Roumba, the position is won, and a starting position that can be played to that end is a winning board. Once the total number of marbles is fixed, the winning board is always unique. For 4 marbles the only winning board is , , .
Given the total number of marbles , find the winning board.
Input
The first line holds the number of test cases . ()
Each of the next lines holds one test case: the case number and the total number of marbles placed on the board, separated by a space. (, )
The bound 2117 is the largest whose winning board reaches no farther than pit 80.
Output
For each test case, print on one line the case number and the number of the rightmost pit that holds at least one marble.
Starting on the next line, print through of the winning board in order, ten numbers per line separated by single spaces, with only the remaining numbers on the last line.
never exceeds 80 in any test case.