Lv.1mathnumber-theorysieve
Divisor Counts
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an array nums of positive integers, return the number of divisors of each nums[i], in the same order.
For example, 12 has the 6 divisors 1, 2, 3, 4, 6 and 12.
Examples
| nums | result |
|---|---|
[1, 6, 12] | [1, 4, 6] |
[7, 36, 100] | [2, 9, 9] |
[5000000] | [56] |
Example 3 — has divisors.
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(nums: int[]) → int[]
Constraints
-
nums.length -
nums[i]
Expected complexity
- Time
- O(M log log M + Q log M)
- Space
- O(M)
Examples
| nums | Expected |
|---|---|
[1,6,12] | [1,4,6] |
[7,36,100] | [2,9,9] |
[5000000] | [56] |
Hint
Hint
If , it has divisors. Precompute the smallest prime factor
spf[x] of every number up to the maximum with a sieve; then dividing by spf[x] repeatedly factors x in at most
steps. Trial division up to for each of 200,000 values exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 248 ms | estimated |
| C++ (GCC, gnu++23) | 248 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1490 ms | derived |
| Kotlin 2.4 (JVM) | 1496 ms | estimated |
| C# 14 (.NET 10) | 1496 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 3 | verdict only |