Lv.2binary-searchmathparametric-search
K-th Smallest in a Multiplication Table
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
An m × n multiplication table has i × j in row i, column j (, ).
Return the k-th smallest of its entries (equal values in different cells are counted separately).
Examples
| m | n | k | result |
|---|---|---|---|
3 | 3 | 5 | 3 |
2 | 3 | 6 | 6 |
1 | 1 | 1 | 1 |
Example 1 — Sorted entries: 1, 2, 2, 3, 3, 4, 6, 6, 9; the 5th is 3.
Example 2 — 1, 2, 2, 3, 4, 6; the 6th is 6.
Example 3 — A single cell.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(m: int, n: int, k: int) → int
Constraints
-
m,n -
k
Expected complexity
- Time
- O(m log(mn))
- Space
- O(1)
Examples
| m | n | k | Expected |
|---|---|---|---|
3 | 3 | 5 | 3 |
2 | 3 | 6 | 6 |
1 | 1 | 1 | 1 |
Hint
Hint
Do not build or heap-walk the table (up to cells). Count instead: row i has min(n, ⌊x/i⌋) entries ≤ x, so
count(x) takes . It is non-decreasing in x, so binary search the smallest x in [1, m·n] with count(x) ≥ k.
Total .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 4 | verdict only |