ICPC North America Qualifier 2016, Problem I
Primonimo is a game played on an board filled with numbers taken from the range for some prime number . At each move, a player selects a square and adds to the numbers in all squares in the same row and column as the selected square. If a square already shows the number , it wraps around to 1.
The game is won if all squares show . Given an initial board, find a sequence of moves that wins the game!
Input Specification
The input consists of a single test case. The first line contains three numbers denoting the number of rows , the number of columns , and a prime number . Each of the next lines consists of numbers in the range .
Output Specification
If a winning sequence of at most moves exists, output an integer denoting the number of moves in the sequence. Then output moves as a sequence of integers that numbers the board in row-major order, starting with . If there are multiple such sequences, you may output any one of them. If no winning sequence exists, output -1
.
Sample Input 1
4 5 5
2 1 1 1 2
5 3 4 4 3
4 3 3 3 2
3 1 3 3 1
Sample Output 1
6
19 12 2 18 5 5
Sample Input 2
3 3 3
3 1 1
1 3 2
3 2 3
Sample Output 2
13
4 2 6 1 9 7 5 5 7 1 2 3 3
Sample Input 3
3 2 2
1 2
2 1
1 2
Sample Output 3
-1
Sample Input 4
3 2 2
2 1
2 1
1 1
Sample Output 4
1
6
Comments