eolymp
bolt
Try our new interface for solving problems
Problems

Game of Squares

Game of Squares

Alice and Bob are playing with \textbf{k}-dimensional rectangular parallelepiped consisting of \textbf{n_1}×\textbf{n_2}×...×\textbf{n_k}. unit hypercubes. They make moves in turn. Alice chooses some unit hypercube, and cuts the parallelepiped through the center of this hypercube with all possible planes that are parallel to its sides. All unit hypercubes that were cut by at least one plane are removed, and what remains is a series of smaller rectangular parallelepipeds. It is required that at least one of those small parts has edge lengths that are pairwise relatively prime with the corresponding edge lengths of the original parallelepiped. It is also allowed to cut the parallelepiped in such way that no parts remain. Afterwards, each player chooses any of remaining parallelepipeds, and cuts it as described above. After a cut every parallelepiped is left, and making the turn the player can choose any of them. The player who can't move loses. Assuming both players play optimally, who will win? \includegraphics{https://static.e-olymp.com/content/0a/0afeecde551414cc22d9b124a7823da2fe0376e9.jpg} Let's consider an example. Assume that \textbf{k=2}, and we have a \textbf{6}×\textbf{5} rectangle. By cutting the hypercube at (\textbf{1}, \textbf{4}), we get two parts: \textbf{5}×\textbf{1} and \textbf{5}×\textbf{3}. As the second part's (as well as first part's, but that's not important) edge lengths are relatively prime with the edge lengths of the original rectangle (\textbf{5} is relatively prime with \textbf{6} and \textbf{3} is relatively prime with \textbf{5}), this is a possible move. However, cutting the hypercube at (\textbf{3}, \textbf{2}) is not a possible move, because each of the remaining parts (\textbf{2}×\textbf{1}, \textbf{3}×\textbf{1}, \textbf{2}×\textbf{3}, \textbf{3}×\textbf{3}) doesn't satisfy the condition above. \InputFile The first line of the input contains an integer \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{8}), the second line contains \textbf{k} integers \textbf{n_1}, \textbf{n_2}, ..., \textbf{n_k}. \textbf{1} ≤ \textbf{(n_1+1)}×\textbf{(n_2+1)}×...×\textbf{(n_k+1)} ≤ \textbf{10000}. \OutputFile Output the number of the winning player to the first line of output (\textbf{1} for Alice, \textbf{2} for Bob). In case Alice wins the game, output the first move that leads her to the win to the second line of output. The move is described by \textbf{k }numbers, \textbf{1}-based coordinates of the cut hypercube. In case there're several possible moves that lead her to the win, output the lexicographically smallest one.
Time limit 5 seconds
Memory limit 64 MiB
Input example #1
2
2 2
Output example #1
2
Author Dmitry Gozman
Source Dmitry Gozman Contest 1, Petrozavodsk training camp, January 2007