Prime ranges
Level B. Teaches std.math.Sieve and prefix counts.
You are given q queries (1 ≤ q ≤ 2·10⁵), each two numbers 1 ≤ l ≤ r ≤ 10⁵. For each, print how many primes lie between l and r, inclusive.
Input: q, then q lines l r. Output: one line per query.
| Input | Output |
|---|---|
21 1010 20 | 44 |
5 tests · Ctrl+Enter
Hints
Sieve(N)fromstd.mathbuilds the table once, in O(N log log N).sv.is_prime(k)is then a lookup.Prefix(...)builds the prefix sums once, above the queries, andpre.fold(l..=r)is the number of primes inl..=rin O(1). Writing thepre[k] = pre[k - 1] + ...loop yourself is what the hand_rolled_prefix_sum lint flags.SieveandPrefixare in the prelude, so nouseis needed; the math reference and the library guide list the rest.