P2049 Magic Chess Piece

Description

In an $M \times N$ magic chessboard, each cell contains an integer. When a chess piece steps into a cell, the number on the piece is multiplied by the number in that cell. A chess piece moves from the top-left corner to the bottom-right corner, and it can only move right or down. After it reaches the bottom-right corner, what values modulo $K$ can the number on the piece be? For the following $2 \times 3$ board: ``` 3 4 4 5 6 6 ``` The piece starts with the number $1$, enters the board from the top-left, and moves to the bottom-right. In the example above, the final number on the piece can be $288, 432$, or $540$. Therefore, when $K = 5$, the final possible results are: $0, 2, 3$.

Input Format

The first line contains three integers, $M,N,K (1 \leq M,N,K \leq 100)$. The next $M$ lines each contain $N$ integers, representing the numbers in the matrix.

Output Format

The first line contains the count of possible results. The second line contains all possible results in ascending order.

Explanation/Hint

Translated by ChatGPT 5