P7784 「MCOI-Zero / AC6-M15」 Chandelier
Background
"No, it’s not over yet..."
"What!?"
"The barrel is open.
That ugly thing has started to dissipate heat through the barrel!"
"Its critical area is right behind the barrel."
"Talisman...
...we can trust you, right?"
$$_{{\frac{\large\text{ACE COMBAT }\Large6}{\tiny{\text{F i r e s\quad O f\quad L i b e r a t i o n}}}}}\\ \text{Mission 15} \\\Large\text{Chandelier}\\\tiny -\textit{ To All Things }
-$$

Description
The core of Chandelier can be described as an $n \times m$ matrix. Initially, every cell in the matrix is an independent space.
If there exist two spaces in the core such that their union on the matrix is a rectangle, then these two spaces can be merged into one space, and the merged space is exactly the union of the original two spaces.

If there do not exist any two spaces whose union is a rectangle, then we say the core has reached a stable state.
If the length of a space reaches $n$ or its width reaches $m$, then the core will short-circuit and explode. **Note that the length here means the side length parallel to the side of the original matrix whose length is $n$, and similarly for the width. In other words, if a side parallel to the side of the original rectangle with length $n$ reaches $m$ and $m < n$, then it is legal.**
Only after the core reaches a stable state can you attack and destroy it. The required number of attacks equals the number of remaining spaces.
You do not have much ammo left, so you can only attack $10$ times.
You need to control the merges properly so that the core reaches a stable state without short-circuiting, and the required number of attacks is $\leq 10$.
Find one possible final shape after merging. If the core cannot reach a stable state without short-circuiting, or cannot be merged into $\leq 10$ spaces, output $-1$.
Input Format
One line containing two integers $n, m$ separated by spaces.
Output Format
If there is no solution, output one line `-1`.
If there is a solution, first output an integer $x$, representing the number of spaces.
Then output an $n \times m$ matrix $a$, satisfying $1 \leq a_{i,j} \leq x$. $a_{i,j}$ indicates which space $(i, j)$ belongs to. The same number means the same space, and different numbers mean different spaces.
If there are multiple solutions, output any one.
Explanation/Hint
- Subtask 1 (20 pts): $n, m \leq 2$.
- Subtask 2 (40 pts): $n, m \leq 4$.
- Subtask 3 (40 pts): no special constraints.
For $100\%$ of the testdata, $1 \leq n, m \leq 10^{3}$.
idea: Sol1, solution: Sol1, code: Sol1, data: Sol1
---
"I finally saw the cannon come to an end..."
"All aircraft, pay attention.
The war is over."
...
"Talisman, look, the sun has risen."
"Even though the night is so long, dawn will come in the end."
"Our comrades we lost, all offered the fire of their lives
to the light of daybreak."
"We did our best to survive—
that is the best memorial for them."
"Now, come on."
"Let’s go home."
Translated by ChatGPT 5