$\lfloor{\frac{a}{b}}\rfloor\bmod c$

55 Views Asked by At

$~a~$ is sum of finite GP series$$1 + x + x^2+ .. +x^n,\qquad n\lt 10^{18}$$ $(0\lt b\lt 10^3)~\text{and}~(0\lt c\lt 10^9)$

I have used, $~a\bmod b = a - b\lfloor{\frac{a}{b}}\rfloor$

$\implies \lfloor{\frac{a}{b}}\rfloor = {\frac{(a - a\bmod b)}{b}}$

I could solve it when the $~\gcd(b,c)=1$, by finding multiplicative inverse.

But, if $~\gcd(b,c)\neq1~$, then how to approach the problem?

1

There are 1 best solutions below

0
On BEST ANSWER

If $a=qbc+rb+s$ with $0\le s\le b-1$ and $0\le r\le c-1$, you want to find $r$. Namely, $qc+r=\lfloor \frac ab\rfloor$ and $r=(qc+r)\bmod c$. To do so, note that $0\le rb+s\le bc-1$ so that $rb+s=a\bmod{bc}$, from which we find $r=\lfloor \frac{a\bmod{bc}}b\rfloor$.

The computattion $a\equiv \frac{x^n-1}{x-1}\pmod {bc}$ is easy if $\gcd(x-1,bc)=1$, but that need not be the case. We can use a recursion on $a_n:=1+x+\cdots +x^n$ directly, noting that $$a_{2k}=(x^k+1)\cdot a_k\qquad \text{and}\qquad a_{k+1}= x\cdot a_k. $$ Using this, you find $a\bmod{bc}$ in $O(\log n)$ multiplications modulo $bc$. For these modular multiplications, you may want to be able to handle $80$ bit unsigned integer multiplications (because $bc$ can be almost $2^{40}$, but not more) for intermediate results. Or work in a mixed base representation and define addition and multiplication $\bmod {bc}$ for ordered pairs $\langle u,v\rangle$ with $0\le u<c$, $0\le v<b$, representing the number $ub+v\bmod{bc}$.