PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
Chef wants to bake a 2-layer cake.
The first layer must receive a color from \{1, 2, \ldots, A\}, and the second layer from \{1, 2, \ldots, B\}.
How many ways are there to choose the colors such that both layers receive different colors?
EXPLANATION:
Suppose A \leq B.
The first layer can receive any color from \{1, 2, \ldots, A\}, for A choices in total.
The second layer can receive any color from \{1, 2, \ldots, B\}, except the color given to the first layer - for (B-1) choices in total.
Note that since A \leq B, the color chosen for the first layer will always be present in \{1, 2, \ldots, B\}. This is why the A \leq B condition we assumed initially was important.
So, the total number of ways in this case is A\cdot (B-1).
What about the case where A \gt B?
If you try to do the same analysis here, you will run into some casework: while there are A choices for the first color, there are either B or B-1 choices for the second color, depending on whether the first color was \leq B or not.
So, a simple multiplication doesn’t seem to work here, unlike the previous case.
However, we can simply change our perspective a bit: choose the color of the second layer
first instead (B choices), and then there are always (A-1) choices for the first layer, for
B\cdot (A-1) choices in total!
A single-line formula for the answer exists too, bringing both cases into one - that being
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
a, b = map(int, input().split())
print(min(a, b) * (max(a, b) - 1))