So, I'm trying to research a formula for my programming problem which is to calculated :
$$\sum_{i= 0}^{n - 1} \bigg\lfloor\frac{ai + b}{m}\bigg\rfloor$$
Because the range of $n$ is kinda big, so to calculated that sum, I have to analysis the above formula in order to make the code run faster.
The problem here is, I have found the code which solve the things, but I hardly can understand it, I hope everyone can explain me why the sum can be analysis like that, and if you having another ways to do it, it will be very helpful for me.
The code : https://ideone.com/u8uUAP.
Describe in math formula : Let ${x}'_y = (x)mod(y)$ ; $A = \lfloor\frac{{a}'_mn+{b}'_m}{m}\rfloor$ ; $B = Am - {b}'_m$
Then $$\sum_{i = 0}^{n - 1} \bigg\lfloor\frac{ai + b}{m}\bigg\rfloor = \sum_{i = 0}^{n - 1} \bigg\lfloor{\frac{a}{m}}\bigg\rfloor i + \sum_{i = 0}^{n - 1} \bigg\lfloor{\frac{b}{m}}\bigg\rfloor + \bigg(n - \bigg\lceil{\frac{B}{{a}'_m}}\bigg\rceil\bigg)A + \sum_{i = 0}^{A} \bigg\lfloor{\frac{mi + ({{a}'_m - {B}'_{{a}'_m})}'_{{a}'_m}}{{a}'_m}}\bigg\rfloor$$
Sorry for the bad english, and thank you very much for reading.