Given only the counts of squares cut per turn, recover the smallest possible long side L of the original rectangle.
Medium7MathNumber theoryGreedyNo attempts yetTime limit2sMemory limit512 MBTwo players take turns cutting one rectangular sheet of paper. The long side of the sheet has length L and the short side has length W.
On a turn, a player cuts the largest square that fits in the rectangle that is left. In a rectangle whose short side is W, that square has side W. The player cuts one or more squares of that size, every square cut in the turn has the same size, and what remains after the cut must be a single rectangle. The player who cuts the paper with nothing left over wins.
A game is recorded as L W r a1 a2 … ar, where r is the number of turns and ai is the number of squares cut on turn i. For example, on a sheet with long side 5 and short side 2, the first player cuts two squares of side 2 and leaves a 2×1 rectangle, then the second player cuts two squares of side 1 and the game ends. The record of that game is 5 2 2 2 2.
A storage fault erased the first two numbers, L and W, from every record. Recover L from the surviving r and a1 through ar. When several values of L are possible, answer with the smallest one.
The first line contains the number of records M.
Each of the next M lines holds one record. The line starts with the number of turns r, followed by r integers a1 through ar separated by spaces.
Print M lines. On line i, print the smallest long side L among the rectangles that produce record i.