Chessboard
Limits: 2 sec., 512 MiB
This statement was translated automatically from Ukrainian.
There is an \(n \times m\) chessboard (\(n\) rows and \(m\) columns). The top-left cell of the board is white, and Zenik is standing on it.
Zenik wants to get from the top-left corner to the bottom-right corner, but Zenik can only move on white cells. Initially, Zenik can choose some rows and invert the colors of all cells in the chosen rows. After that, he can choose some columns and invert the colors of all cells in the chosen columns.
For example, if Zenik applies this operation to the third row, it looks like this:
Note that since Zenik only moves on white cells, after his operations the top-left cell must remain white. What is the minimum number of rows and columns Zenik needs to choose to reach the bottom-right corner?
Input
The first line contains two integers \(n\) and \(m\) — the height and width of the chessboard.
Output
Print a single integer — the minimum number of rows and columns to change in order to reach the bottom-right cell.
Constraints
\(1 \le n, m \le 10^9\).
Scoring consists of the following blocks:
1 point for each example from the statement,
40 points: \(n = 1\),
58 points: no additional constraints.
You will get the points for a block only if your program passes all tests in that block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 1 5 | 2 |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 | 2 |
Notes
In the first example, Zenik can choose the second and fourth columns.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Chessboard - Editorial
We need to find the minimum number of row and column flips needed to be able to walk along white cells from the top right cell to the bottom left one. To repaint the board optimally, it is enough to first flip all even rows and then all even columns. After these operations the whole board becomes white. So the answer is \(\lfloor \frac{n}{2} \rfloor + \lfloor \frac{m}{2} \rfloor\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|