Digit Sums Divisible by K
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given positive integers lo, hi and k, return how many integers x with lo ≤ x ≤ hi have a decimal digit
sum divisible by k. For example, 4817 has digit sum 4 + 8 + 1 + 7 = 20, so it counts when k = 5.
Examples
| lo | hi | k | result |
|---|---|---|---|
1 | 20 | 3 | 6 |
10 | 99 | 10 | 9 |
7 | 7 | 2 | 0 |
Example 2 — 19, 28, 37, 46, 55, 64, 73, 82, 91 (a two-digit number's digit sum is at most 18).
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(lo: long, hi: long, k: int) → long
Constraints
-
lohi -
k - The answer fits in a 64-bit integer.
Expected complexity
- Time
- O(D * K * 10)
- Space
- O(D * K)
Examples
| lo | hi | k | Expected |
|---|---|---|---|
1 | 20 | 3 | 6 |
10 | 99 | 10 | 9 |
7 | 7 | 2 | 0 |
Hint
Hint
Count f(N) = qualifying numbers in [0, N] and answer f(hi) - f(lo - 1). Build N digit by digit from the front,
tracking the digit sum modulo k and whether the prefix still equals N's prefix; once a smaller digit is chosen, the rest
is free (precompute ways[r][m] for r free digits reaching remainder m). Checking every number in the range exceeds
the time limit; with digits is enough.
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 | 13 | verdict only |
| performance | 60 | 6 | verdict only |