#P2049. Magic Chessboard

    ID: 15331 Type: Default 1000ms 256MiB

Magic Chessboard

Magic Chessboard

Given an $M \times N$ magic chessboard where each cell contains an integer, a chess piece starts with the number $1$ at the top-left cell. Each time the piece enters a cell, its number is multiplied by the integer in that cell. The piece can only move right or down, and it must reach the bottom-right cell. Your task is to determine all the distinct possible remainders when the final number is taken modulo $K$.

For example, consider the following $2 \times 3$ board:

3  4  4
5  6  6

Starting from $1$, the possible final numbers are $288$, $432$, or $540$. Thus, for $K = 5$, the possible remainders are 0, 2, and 3.

inputFormat

The first line contains three integers $M$, $N$, and $K$, where $M$ and $N$ denote the dimensions of the board and $K$ is the modulo value. The following $M$ lines each contain $N$ integers representing the board's cells.

For example:

2 3 5
3 4 4
5 6 6

outputFormat

Output all distinct possible remainders (modulo $K$) in increasing order, separated by a single space.

sample

2 3 5
3 4 4
5 6 6
0 2 3