#P6483. Find Grid Dimensions from Cell Counts

    ID: 19697 Type: Default 1000ms 256MiB

Find Grid Dimensions from Cell Counts

Find Grid Dimensions from Cell Counts

We are given a grid with n rows and m columns. In this grid, the outer border cells are colored red and all the remaining inner cells are colored black. The number of red cells is given as \(r\) and the number of black cells is given as \(b\). Your task is to find one pair of positive integers \(n\) and \(m\) that produce exactly \(r\) red cells and \(b\) black cells.

Explanation:

If \(n \ge 2\) and \(m \ge 2\), the cells on the border (red cells) can be computed as:

[ \text{red} = 2n + 2m - 4 ]

and the inner black cells as:

[ \text{black} = (n-2)(m-2). ]

Thus, the following equations must hold:

[ 2n+2m-4 = r \quad \text{and} \quad (n-2)(m-2) = b. ]

Note that if \(b = 0\), the grid has no inner cells, which can happen when one of the dimensions is 1. In that case, you can take \(n = 1\) and \(m = r\) (or vice versa).

You only need to output one valid solution \((n, m)\).

inputFormat

The input consists of two integers \(r\) and \(b\) separated by a space on one line.

\(r\) denotes the number of red (border) cells and \(b\) denotes the number of black (inner) cells.

outputFormat

Output two integers \(n\) and \(m\) separated by a space, representing a valid grid size (number of rows and columns, respectively) that yield exactly the given counts of red and black cells.

sample

8 1
3 3