#P16119. [USTCPC 2026] Gradient Descent

[USTCPC 2026] Gradient Descent

Background

"Phew, I finally figured out the formula for the discrete gradient!" Kruskal-chan stretched lazily, and her notebook in front of her was covered with grids.

A classmate sitting next to her leaned over. "Are you still working on that gradient descent problem, Kruskal-chan?"

"Yes! I'm thinking about how to make the algorithm run faster while still guaranteeing that it finds the minimum." Kruskal-chan tilted her head, looking puzzled.

"Ah, so you want the maximum learning rate? But you also have to consider boundary cases." the classmate replied casually.

Kruskal-chan's eyes lit up. "Right! Let's calculate it!"

Problem Description

For a grid scalar field ff, define the discrete gradient at (i,j)(i, j) 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)(i, j) lies on the boundary of the grid, compute the corresponding one-sided difference only, and there is no need to divide by 22. For example, for a point with i=1i=1: ΔfΔx=fi+1,j−fi,j\frac{\Delta f}{\Delta x}=f_{i+1,j}-f_{i,j}.

Use the following gradient descent algorithm 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 that the step size is always an integer, η\eta must be even. If, during gradient descent, the coordinates go out of the grid range, the process ends immediately.

Given an n×mn\times m grid scalar field and a starting coordinate (the starting coordinate is guaranteed not to be the global minimum), what is the maximum learning rate that can be used while still guaranteeing that the global minimum can be found?

As long as the gradient descent process passes through a position that attains the global minimum value, it is considered to have found the global minimum.

Input Format

The first line contains an integer TT (1≤T≤250001\le T\le 25000), the number of test cases.

For each test case, the first line contains four integers: the number of rows nn (n≥2n\ge 2), the number of columns mm (m≥2m\ge 2), the starting row rr (1≤r≤n1\le r\le n), and the starting column cc (1≤c≤m1\le c\le m). It is guaranteed that nm≤105nm\le 10^5.

Then follow nn lines, each containing mm integers. The jj-th number in the ii-th line denotes fi,jf_{i,j} (∣fi,j∣≤100\lvert f_{i,j}\rvert\le 100). It is guaranteed that fr,c≠min⁡ff_{r,c}\neq\min f.

It is guaranteed that ∑nm≤105\sum nm\le 10^5.

Output Format

Output TT lines, one for each test case, giving the answer. If it is impossible to find the global minimum no matter what the learning rate is, output Impossible. Otherwise, output the maximum learning rate that can find the global minimum.

Note: In this problem, the learning rate must be a positive even integer.

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
Impossible
4

Hint

For the first sample, since the gradient at the starting position is 00, the coordinates will never change, so it is impossible to reach a position that attains the global minimum.

For the second sample, when η=4\eta=4, the coordinates change as: (1,3)→(5,1)→(5,5)(1,3)\to(5,1)\to(5,5). At this point, the global minimum value 11 is reached, so η=4\eta=4 works. Any learning rate greater than 44 will cause the first gradient descent step to go out of the grid range, so the answer is 44.

Translated by ChatGPT 5