#P11168. Minimum Grid Point Selection
Minimum Grid Point Selection
Minimum Grid Point Selection
Given an (n \times m) grid, select the minimum number of grid points such that for any two distinct grid points, the straight line segment connecting them (which includes both endpoints) passes through at least one selected point. In other words, there cannot exist two grid points which are both unselected. Additionally, you are given three integers (a), (b), and (x) specifying a constraint on the cell at row (a) and column (b) (1-indexed):
[
\begin{cases}
\text{if } x = 0, ; \text{the cell cannot be selected,}\
\text{if } x = 1, ; \text{the cell must be selected.}
\end{cases}
]
Determine the minimum number of grid points that must be selected.
Note: In a grid of size 1 (i.e. one cell), there are no pairs of distinct grid points; hence if no cell is forced to be selected, the answer can be 0.
inputFormat
The first line contains two integers (n) and (m) (1 ≤ n, m ≤ 109), representing the number of rows and columns of the grid respectively.
The second line contains three integers (a), (b), and (x) where (a) and (b) denote the row and column (1-indexed) of the cell with a constraint, and (x) is either 0 or 1 with the meanings described above.
outputFormat
Output a single integer — the minimum number of grid points that must be selected to satisfy the condition.
sample
1 1
1 1 0
0