#P1790. Partitioning a Grid with Border Constraints
Partitioning a Grid with Border Constraints
Partitioning a Grid with Border Constraints
You are given a rectangle of size a × b consisting of small square cells. The rectangle has a rows and b columns, with the constraints \(1 \le a \le 6\) and \(2 \le b \le 6\). You have to partition the rectangle into two non-empty parts by assigning each cell to one of the two parts.
The partition must satisfy the following additional condition: in each part there must be at least one cell that lies on the border of the original rectangle (i.e. in the outermost layer of cells).
Note that the two parts are unlabeled, meaning that a partition and its swapped version are considered identical.
Objective: Count the total number of ways to split the rectangle according to the above rule.
Hint: You can think of selecting a non-empty proper subset of cells as forming one part. However, ensure that both the selected subset and its complement each contain at least one border cell. Since the parts are unlabeled, every valid split is counted only once.
Mathematical Formulation:
Let the total number of cells be \(T = a \times b\). Let \(I\) be the number of interior cells (i.e., those that are not on the border). Note that \(I = \max(0, a-2) \times \max(0, b-2)\). The total number of ways to choose a non-empty proper subset is \(2^T - 2\). However, the invalid cases are when one part does not contain any border cell. There are exactly \(2^{I} - 1\) ways to choose a subset consisting exclusively of interior cells, and similarly for its complement. Thus, the total valid splits is given by: \[ \text{Answer} = \frac{2^T - 2 - 2(2^{I} - 1)}{2} = 2^{T-1} - 2^{I} \]
inputFormat
The input consists of a single line containing two integers a and b, representing the number of rows and columns of the rectangle respectively.
\(1 \le a \le 6,\ 2 \le b \le 6\).
outputFormat
Output a single integer, the total number of valid ways to partition the rectangle into two parts that satisfy the given border condition.
sample
1 2
1