Choose tables from the workload
Combine prime prefixes, exponent summaries, and divisor tables.
Understand the idea
For prime-count intervals, build flags and a prefix count. For divisor counts of every x ≤ N, let each d contribute to d, 2d, 3d, ... . For the factorization of a huge product, add prime exponents from its inputs instead of constructing the product.
Trace a small example
Eight primes lie through 20 and four through 10, so [11,20] contains four. Combining 12 = 2² × 3 and 18 = 2 × 3² gives 2³ × 3³, whose divisor count is 16.
Conditions and cost
Prime prefixes give O(1) queries after preparation. Visiting all multiples costs O(N log N) by the harmonic sum N/1 + ... + N/N. Build reusable tables once and reset query-specific state.
Prefix sum explorer
Query [l, r] is zero-based and inclusive. P[k] stores the total of the first k values; P[0] is zero.
Input array a
Prefix array P
Preprocess once in O(n). Each range sum then takes O(1).
Guided simulation · Change the inputs to test your understanding.
How do prime exponents combine for a product?
Turn the idea into code
With P[x] counting primes ≤ x, write the count in [L,R] for L ≥ 1.
Give me a hint
Remove the prefix ending before L.
Show the explained solution
P[R] − P[L − 1], provided both indices lie within the built table.
Your notes
Mark complete when you can explain it yourself.