Code RoomGeometric series modulo
HardPrep Room Coding #1209

Geometric series modulo

CodingAlgorithms & data structuresSenior–Staff~30 min

Compute S = (1 + a + a^2 + ... + a^(b-1)) mod m, the sum of a geometric series with b terms, where 0 <= a, 0 <= b <= 10^18, and 1 <= m <= 10^9. Use a divide-and-conquer recurrence so it runs in O(log b) modular operations (do NOT loop b times). If b == 0 the sum is 0.

Implement
geo_series_mod(a: int, b: int, m: int) → int
Examples
in[2,4,1000000007]out15
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[2,4,1000000007]15not run yetsample