#P7243. 最大公约数
最大公约数
Description
On the contrary, social circles come in all kinds of shapes, and the common divisors are pitifully small. It seems hard to keep one’s own personality, and thus one becomes a boring person.
Now abstract interpersonal relationships into an rectangle. Each person’s initial personality is . Starting from the second day, each person will build relationships with the four people up, down, left, and right (if they exist), and their personality becomes the greatest common divisor of their own personality yesterday and the personalities of the surrounding people. Then, for the person at row and column , after how many days will their personality become ?
Simplified statement
There is an matrix . Define one transformation on a matrix as: change every element in the matrix into the greatest common divisor of itself and its four adjacent elements (up, down, left, right; ignore non-existing ones). Ask for the minimum number of transformations needed for to become . If can become after some number of transformations, output the minimum number; otherwise output .
Input Format
The first line contains two integers , indicating the number of rows and columns of the matrix.
The next lines each contain integers. The integer in row and column describes the initial personality of the person at that position.
The next line contains two integers , indicating the position of a specified person.
Output Format
Output one integer , meaning that the personality of the person at row and column will become after days. In particular, if it will never become , output .
2 2
2 2
1 2
2 1
0
2 2
2 2
2 2
1 1
-1
3 3
3 2 3
2 3 2
3 2 3
2 2
1
Hint
Sample explanation 3
The personality matrix on the first day (i.e. the initial matrix) is
$$\begin{pmatrix} 3&2&3\\ 2&3&2\\ 3&2&3 \end{pmatrix}$$The personality matrix on the second day is
$$\begin{pmatrix} 1&1&1\\ 1&1&1\\ 1&1&1 \end{pmatrix}$$It can be seen that after only one day, becomes , so the answer is .
Constraints and conventions
This problem uses bundled tests.
For of the testdata, , , , .
| Subtask | Score | Special constraints | |
|---|---|---|---|
| 1 | 1 | / | Guarantee that the personality at the given position will never become |
| 2 | Guarantee that | ||
| 3 | / | ||
| 4 | 10 | ||
| 5 | 30 | ||
| 6 | 10 | / | Guarantee that for all |
| 7 | Guarantee that both and equal | ||
| 8 | 35 | / | |
Translated by ChatGPT 5
京公网安备 11011102002149号