H. Pencil Game

Codeforces
IDCF10054H
Time3000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Minh has a box of pencils. The box is a rectangle of size M * N, where position (i, j) has a pencil with a length of exactly i * N + j (0 ≤ i ≤ M - 1,  0 ≤ j ≤ N - 1). Note that position (0, 0) does not have any pencil hence having a length of 0. He wonders if he could select a sub-rectangle of the box and join all the pencils within that sub-rectangle together, to get a new long pencil that has a specific length L that he wants. Your task is to find a sub-rectangle of the box in which the total length of the contained pencils is L and return the area of that the sub-rectangle. If there are multiple solutions, return the smallest possible area. If there’s no such sub-rectangle, return  - 1. The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 150. The following lines describe the datasets. Each dataset contains three space-separated numbers M, N and L (1 ≤ M, N ≤ 106, 1 ≤ L ≤ 1012) written in one line. For each dataset, write in one line the smallest possible area of the sub-rectangle in which the total sum of pencil lengths is L. Write in one line  - 1 if there is no such sub-rectangle. ## Input The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 150. The following lines describe the datasets. Each dataset contains three space-separated numbers M, N and L (1 ≤ M, N ≤ 106, 1 ≤ L ≤ 1012) written in one line. ## Output For each dataset, write in one line the smallest possible area of the sub-rectangle in which the total sum of pencil lengths is L. Write in one line  - 1 if there is no such sub-rectangle. [samples]
**Definitions** Let $ M, N \in \mathbb{Z}^+ $ denote the dimensions of the grid, with positions $(i, j)$ for $ 0 \leq i \leq M-1 $, $ 0 \leq j \leq N-1 $. The pencil at position $(i, j)$ has length $ i \cdot N + j $. Let $ L \in \mathbb{Z}^+ $ be the target total length. **Constraints** 1. $ 1 \leq M, N \leq 10^6 $ 2. $ 1 \leq L \leq 10^{12} $ **Objective** Find the smallest area $ A = (i_2 - i_1 + 1) \cdot (j_2 - j_1 + 1) $ of a sub-rectangle defined by $ i_1 \leq i \leq i_2 $, $ j_1 \leq j \leq j_2 $, such that: $$ \sum_{i=i_1}^{i_2} \sum_{j=j_1}^{j_2} (i \cdot N + j) = L $$ If no such sub-rectangle exists, return $-1$.
API Response (JSON)
{
  "problem": {
    "name": "H. Pencil Game",
    "description": {
      "content": "Minh has a box of pencils. The box is a rectangle of size M * N, where position (i, j) has a pencil with a length of exactly i * N + j (0 ≤ i ≤ M - 1,  0 ≤ j ≤ N - 1). Note that position (0, 0) does n",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10054H"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Minh has a box of pencils. The box is a rectangle of size M * N, where position (i, j) has a pencil with a length of exactly i * N + j (0 ≤ i ≤ M - 1,  0 ≤ j ≤ N - 1). Note that position (0, 0) does n...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ M, N \\in \\mathbb{Z}^+ $ denote the dimensions of the grid, with positions $(i, j)$ for $ 0 \\leq i \\leq M-1 $, $ 0 \\leq j \\leq N-1 $.  \nThe pencil at position $(i, j)$ has lengt...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments