Official

A - 色紙の正方形 / Shikishi Squares Editorial by admin Editorial for printing

swe-2-high

Algorithm

We can arrange \(\lfloor A/C\rfloor\) rows and \(\lfloor B/C\rfloor\) columns of squares. The answer is

\[\lfloor A/C\rfloor\times\lfloor B/C\rfloor.\]

Compute two integer divisions and multiply their results.

Proof of Correctness

If \(A<C\) or \(B<C\), no square fits, and the formula gives \(0\). Otherwise, both dimensions are at least \(C\).

Take the upper-left corner of the sheet as the origin, and let \((x,y)\) be the upper-left corner of a square. Since it must fit inside the sheet, \(0\le x\le B-C\) and \(0\le y\le A-C\). Classify the square by the integer pair \((\lfloor x/C\rfloor,\lfloor y/C\rfloor)\). There are at most \(\lfloor B/C\rfloor\lfloor A/C\rfloor\) possible pairs. Two squares with the same pair differ by less than \(C\) in both coordinates, so their interiors would overlap. Each pair can therefore contain at most one square, proving this product is an upper bound.

Conversely, placing the squares in a regular grid with \(\lfloor A/C\rfloor\) rows and \(\lfloor B/C\rfloor\) columns attains the bound without overlap or overhang. Thus the formula gives the maximum.

Complexity

Both time and space complexity are \(O(1)\).

posted:
last update: