$P(n)$ and $D(n)$ are two large integers.  

Suppose $R(n) = \frac{P(n)}{D(n)}$ is an integer.  

I want to compute $R(n)\bmod m$.  

$P(n)$ and $D(n)$ are too large to be computed but $P(n)\bmod m$ and $D(n)\bmod m$ can easily be computed.  

For example, how to compute $S(x,k) = \sum\_{k=0}^{n}x^k\mod m$ for a large $n$?  

$S(x,n) = \frac{x^{n+1}-1}{x-1}$  

I know how to do if $x-1$ and $m$ are coprime, using the modular inverse of $x-1$, but what if they are not coprime?  

Thanks in advance 


Philippe



Computing the ratio of two large integers modulo m 
As pointed out in the comments, the problem cannot be solved in the generality you state it in, but the magic words are "chinese remainder theorem". If you have an estimate on the sizes of $P$ and $Q$ you can compute them modulo $m\_1, \dotsc, m\_k, $ where the $m\_i$ are pairwise relatively prime (or just prime), and the product of the $m\_i$ is bigger than $\max P, Q.$ From this you can reconstitute $P/Q,$ but if you do the "reconstitution step" mod $m$ you will get what you want. For more details, just google for "Chinese remainder algorithm".


