#P9629. Harmonious Vertex-Colored Rectangles
Harmonious Vertex-Colored Rectangles
Harmonious Vertex-Colored Rectangles
We are given an (n \times m) grid of points with integer coordinates ((x,y)) such that (1 \le x \le n) and (1 \le y \le m). Each point is colored in one of three colors: red, blue, or yellow. A rectangle is defined by choosing two distinct rows (x_1, x_2) (with (x_1 < x_2)) and two distinct columns (y_1, y_2) (with (y_1 < y_2)). Its four vertices are ((x_1,y_1)), ((x_1,y_2)), ((x_2,y_1)) and ((x_2,y_2)). A vertex-colored rectangle is said to be harmonious if and only if one of the following conditions holds:
- (\color(x_1,y_1)=\color(x_1,y_2)) and (\color(x_2,y_1)=\color(x_2,y_2)), or
- (\color(x_1,y_1)=\color(x_2,y_1)) and (\color(x_1,y_2)=\color(x_2,y_2)).
Here, (\color(x, y)) denotes the color of the point ((x,y)) (colors are identified by numbers, but you may consider them as red, blue, and yellow). Two colorings are considered different if there is any point colored differently.
Your task is to count the number of ways to color all the points of the grid such that there exists at least one harmonious rectangle whose edges are parallel to the axes. Note that if (n=1) or (m=1) there is no rectangle, so the answer in that case is 0.
For example:
- For a (2 \times 2) grid, the only rectangle formed by the whole grid is harmonious for the following colorings (where the grid is represented as (\begin{bmatrix} a & b \ c & d \end{bmatrix})) if either (a=b) and (c=d), or (a=c) and (b=d). It can be verified that there are 15 harmonious colorings in this case.
inputFormat
The input consists of a single line containing two integers \(n\) and \(m\) (separated by a space), representing the number of rows and columns of the grid respectively.
outputFormat
Output a single integer representing the number of colorings that contain at least one harmonious rectangle.
sample
1 1
0
</p>