QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 512 MB Total points: 100

#17797. Gradient Descent

Statistics

For a grid scalar field $f$, the discrete gradient at $(i, j)$ is defined as follows:

$$ \begin{cases} \frac{\Delta f}{\Delta x} = \frac{f_{i+1,j} - f_{i-1,j}}{2} \\ \frac{\Delta f}{\Delta y} = \frac{f_{i,j+1} - f_{i,j-1}}{2} \end{cases} $$

If $(i, j)$ is located on the grid boundary, the one-sided difference is calculated instead, and division by 2 is not required. For example, at a point where $i=1$, $\frac{\Delta f}{\Delta x} = f_{i+1,j} - f_{i,j}$.

The following gradient descent algorithm is used to find the minimum value in the grid:

$$ \begin{cases} i \leftarrow i - \eta \cdot \frac{\Delta f}{\Delta x} \\ j \leftarrow j - \eta \cdot \frac{\Delta f}{\Delta y} \end{cases} $$

Here, $\eta$ is called the learning rate. To ensure the step size is always an integer, $\eta$ must be an even number. If the coordinates exceed the grid boundaries during the gradient descent process, the process terminates immediately.

Given an $n \times m$ grid scalar field and initial coordinates (guaranteed that the initial coordinates are not the global minimum), what is the maximum learning rate that can be set such that the global minimum can be found?

The global minimum is considered found if the gradient descent process passes through a position where the global minimum is attained.

Input

There are multiple test cases. The first line contains an integer $T$ ($1 \le T \le 25000$), representing the number of test cases. Each test case starts with a line containing four integers, representing the number of rows $n$ ($n \ge 2$), the number of columns $m$ ($m \ge 2$), the row of the initial position $r$ ($1 \le r \le n$), and the column of the initial position $c$ ($1 \le c \le m$). It is guaranteed that $nm \le 10^5$. The next $n$ lines each contain $m$ integers, where the $j$-th number in the $i$-th row represents $f_{i,j}$ ($|f_{i,j}| \le 100$). It is guaranteed that $f_{r,c} \neq \min f$. It is guaranteed that $\sum nm \le 10^5$.

Output

Output $T$ lines, each representing the answer for a test case. If the global minimum cannot be found regardless of the learning rate, output Impossible. Otherwise, output the maximum learning rate that allows the global minimum to be found.

Note: The learning rate in this problem must be an even number greater than zero.

Examples

Input 1

2
2 3 1 3
1 2 2
1 1 2
5 5 1 3
1 2 3 3 2
2 3 2 3 3
3 1 1 2 2
2 3 2 1 3
2 1 1 3 1

Output 1

Impossible
4

Note

For the first example, since the gradient at the initial position is 0, the coordinates will remain unchanged forever, and it is impossible to reach the position where the global minimum is attained.

For the second example, when $\eta = 4$, the sequence of coordinates is: $(1, 3) \to (5, 1) \to (5, 5)$. At this point, the global minimum value of 1 is reached, so $\eta = 4$ satisfies the condition. Any learning rate greater than 4 would cause the first gradient descent step to go out of bounds, so the answer is 4.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.