Official

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

gpt-6-astra-medium

Overview

We want to find the maximum number of squares with side length \(C\) cm that can be cut from a rectangle of height \(A\) cm and width \(B\) cm. We count how many squares can fit along the vertical and horizontal directions, respectively, and output their product.

Analysis

To place \(k\) squares vertically, the condition \(kC \leq A\) must be satisfied. Therefore, the number of squares that can be placed vertically is the quotient of \(A\) divided by \(C\), which is \(\lfloor A/C \rfloor\).

Similarly, \(\lfloor B/C \rfloor\) squares can be placed horizontally. Arranging them in a grid allows us to cut out a total of

\(\lfloor A/C \rfloor \times \lfloor B/C \rfloor\)

squares.

For example, when \(A=5,\ B=7,\ C=3\), we can place \(1\) square vertically and \(2\) squares horizontally, so the answer is \(2\).

Area alone is not enough

Simply dividing the area of the rectangle by the area of a square, \(\lfloor AB/C^2 \rfloor\), can lead to incorrect answers.

In the previous example, \(\lfloor 35/9 \rfloor=3\), but in reality only \(2\) squares can be cut out. Even if the leftover area is large enough, a square cannot be placed if the width or height of that leftover area is less than \(C\) cm.

Shifting positions does not increase the count

Consider the position of the top-left corner of each square. If we partition the vertical coordinates into intervals of width \(C\) and the horizontal coordinates into intervals of width \(C\), the number of possible partitions are \(\lfloor A/C \rfloor\) and \(\lfloor B/C \rfloor\), respectively.

If two squares fall into the same partition both vertically and horizontally, the difference between their top-left coordinates in both directions will be strictly less than \(C\), meaning they will inevitably overlap. Therefore, at most one square can be placed in each pair of partitions, and the grid arrangement achieves the maximum possible number of squares.

In the provided code, instead of using division, a loop is used to count how many squares fit by incrementing by \(C\) cm each time. Since the constraints are small, this method is fast enough.

Algorithm

  1. Read \(A, B, C\).
  2. Initialize the vertical count rows and horizontal count cols to \(0\).
  3. Increase x through \(C, 2C, 3C, \ldots\), and increment rows by \(1\) as long as x is less than or equal to \(A\).
  4. Increase y through \(C, 2C, 3C, \ldots\), and increment cols by \(1\) as long as y is less than or equal to \(B\).
  5. Output rows * cols.

Complexity

  • Time complexity: \(O(1+\lfloor A/C \rfloor+\lfloor B/C \rfloor)\)
  • Space complexity: \(O(1)\)

Each loop iterates at most \(1000\) times, which is sufficiently fast.

Implementation Notes

  • To account for cases where squares fit exactly, use x <= a and y <= b as the loop conditions.

  • If \(A<C\) or \(B<C\), the corresponding loop will not execute at all. The count for that direction becomes \(0\), correctly resulting in an answer of \(0\).

  • The answer is at most \(1000 \times 1000=10^6\), which fits within the standard 32-bit int type in C++.

  • Using integer division, the same answer can also be obtained as (a / c) * (b / c). In this case, the time complexity is \(O(1)\).

    Source Code

// 問題文から独立に実装: 何個並ぶかをループで数える
#include <iostream>
int main() {
    int a, b, c; std::cin >> a >> b >> c;
    int rows = 0, cols = 0;
    for (int x = c; x <= a; x += c) rows++;
    for (int y = c; y <= b; y += c) cols++;
    std::cout << rows * cols << std::endl;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: