progress

~/problems / Number theory / Modular arithmetic

Yeast growth over a range of hours

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~15 min

During hour i a bakery's sourdough yeast multiplies by factors[i]. The head baker asks: "by what factor did it grow from hour l through hour r?", i.e. factors[l] · ... · factors[r], modulo MOD = 1_000_000_007.

Write growth_queries(factors: list[int], queries: list[tuple[int, int]]) -> list[int], one answer per (l, r) (0-indexed, inclusive), in order.

growth_queries([2, 3, 5, 7], [(0, 3), (1, 2), (2, 2)])   # [210, 15, 5]
growth_queries([10**12], [(0, 0)])                        # [999993007]   (10**12 % MOD)

Up to 10^5 factors (each 1..10^12, never a multiple of MOD) and 10^5 queries.

⭐ Bonus: multiplying out each range still passes; a speed test with 10^5 long queries earns a star with prefix products and division, where dividing mod a prime means multiplying by a modular inverse. No prefix product is 0 mod MOD, so every inverse exists.

Show hint

pre[0] = 1, pre[i+1] = pre[i] * factors[i] % MOD; the product over [l, r] is pre[r+1] * pow(pre[l], MOD - 2, MOD) % MOD.

Topic: Modular arithmetic. Fast exponentiation, Fermat inverses, nCr mod p with factorial tables.

0:00
Ctrl ' run · Ctrl ↵ submit
esc