Real Recursive Euclidean-like algorithm

Revision en7, by OtterZ, 2025-07-22 06:52:34

In the Blog post from RP-1 we learnt how to calculate $$$\sum_{i = 0}^n {[\frac{A\cdot i + B}{C}]}$$$ in $$$\operatorname{O}(\sqrt n)$$$, then he mentioned that there is a recursive euclidean-like algorithm to calculate it in $$$\operatorname{O}(\log n)$$$. Today, as I've just been introduced to the solution by Leo_W, I would like to write it down here as an extend on codeforces blogs.

1.Theory

We define that $$$\operatorname{f}(n,a,b,c) = \lfloor \frac{A\cdot i + B}{C}\rfloor$$$, we found that:

$$$ \begin{aligned}

\operatorname{f}(n,a,b,c) &= \lfloor \frac{A\cdot i + B}{C}\rfloor\\ &= \sum_{i = 1}^n \sum_{j = 1}^{\infty} [\lfloor \frac{A\cdot i + B}{C}\rfloor \ge j]\\ &= \sum_{j = 1}^{\infty} \sum_{i = 1}^n [\lfloor \frac{A\cdot i + B}{C}\rfloor \ge j] \end{aligned} $$$

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en9 English OtterZ 2025-09-10 10:35:17 3208 (published)
en8 English OtterZ 2025-07-22 06:54:16 12
en7 English OtterZ 2025-07-22 06:52:34 326
en6 English OtterZ 2025-07-22 05:08:39 1 Tiny change: '{C}\rfloor' -> '{C}\rfloor$'
en5 English OtterZ 2025-07-22 05:08:30 98
en4 English OtterZ 2025-07-21 17:04:58 250 Tiny change: ' + B}{C}]} in $\oper' -> ' + B}{C}]}$ in $\oper'
en3 English OtterZ 2025-07-21 16:50:53 407
en2 English OtterZ 2025-04-27 09:47:44 0 (saved to drafts)
en1 English OtterZ 2025-04-27 09:45:53 381 Initial revision (published)