Any good way to calculate $\frac {\alpha ^ n - 1 } {\alpha - 1} \pmod{c}$

88 Views Asked by At

I tried by multiplying modular inverse of denominator to the numerator and then taking modulo $c$, but there are problems when the inverse does not exist.

So is there a good way to solve this problem.

Constraints $$ 1 \le \alpha \le 1e9 $$ $c$ is a prime $$ 1 \le n \le 1e9 $$

1

There are 1 best solutions below

3
On

Set $S_0:=1$ and then recursively $S_k:=\alpha S_{k-1}+1 \pmod c$ for all $k=1,\dotsc,n-1$. The last value $S_{n-1}$ is what you seek.