Harmonic Series Modulo a Prime
InterviewTime limit4sMemory limit1024 MB
Given a prime p and a range l to r, compute the sum of the modular inverses of each i in the range, modulo p.
- Level
Medium5 of 10
- Topics
- Number theory, Math, Prefix sum
- Solved
- No attempts yet
Statement
In mathematics, the harmonic series is the following sum:
The series has many remarkable properties. It diverges, and the sum of its first terms approaches as goes to infinity, where is the Euler-Mascheroni constant.
Tor does not care much for mathematical analysis, so the remarkable properties of the harmonic series do not interest him much. Out of boredom, Tor invented a new mathematical object and named it the harmonic series modulo a prime .
Tor defines the harmonic series modulo a prime as the following sum:
For an integer , Tor calls the integer such that . If is prime, then for every such there is exactly one suitable , and this can be proven.
Tor wants to know the value of the sum of terms from to , that is, .
Input
The first line contains three integers , , and (, , , is prime).
Output
Print one integer, the answer to the problem. All arithmetic operations in this problem are performed modulo .
Hint
A natural number is prime if and has no divisors other than and .