类欧几里得算法
这种东西。。。了解了解愉悦一下身心吧。只学了最简单的一种,其他的一坨扩展等哪天心情好了再看。
设(f(n, a, b, c) = sum_{i=0}^n lfloor frac{ai + b}{c} floor)
我们要计算的就是(f(n, a, b, c)),如果认为(n, a, b, c)同阶的话,我们可以做到(log n)的复杂度
前置知识
一些关于取整的小结论
(a < lfloor frac{b}{c} floor Leftrightarrow ac < b)
(a > lceil frac{b}{c} ceil Leftrightarrow ac > b)
(a leqslant lfloor frac{b}{c} floor Leftrightarrow ac leqslant b)
(a geqslant lceil frac{b}{c} ceil Leftrightarrow ac geqslant b)
(lfloor frac{b}{c} floor = lceil frac{b-c+1}{c} ceil)
(lceil frac{b}{c} ceil = lfloor frac{b+c-1}{c} floor)
然后就可以推柿子啦
神仙推导
[egin{aligned}
f(n, a, b, c) &=sum_{i=0}^n lfloor frac{ai + b}{c}
floor\
&=sum_{i=0}^{n} sum_{j=0}^{lfloor frac{ai + b}{c}
floor - 1} 1 \
&=sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} sum_{i=0}^n (j < lfloor frac{ai+b}{c}
floor)\
&=sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} sum_{i=0}^n (j < lceil frac{ai+b-c+1}{c})\
&=sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} sum_{i=0}^n (cj < ai + b - c + 1)\
&=sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} sum_{i=0}^n (i > lfloor frac{cj-b+c-1}{a}
floor)\
&=sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} n - lfloor frac{cj-b+c-1}{a}
floor\
&=n * lfloor frac{an+b}{c}
floor - sum_{j=0}^{lfloor frac{an+b}{c}
floor -1} lfloor frac{cj-b+c-1}{a}
floor\
&=n * lfloor frac{an+b}{c}
floor - f(lfloor frac{an+b}{c}
floor -1, c, c-b-1, a)
end{aligned}
]
然后就能递归算了,每次范围会至少折半,因此复杂度为(log n)