要求以下函数:
[f(a, b, c, n) = sum_{i = 0}^n lfloor frac{ai + b}{c}
floor
]
设(a = k_1 imes c + a', b = k_2 imes c + b'),则
[lfloor frac{ai + b}{c}
floor = lfloor frac{(k_1 imes c + a') imes i + (k_2 imes c + b')}{c}
floor = lfloor frac{a'i + b'}{c}
floor + k_1 imes i + k_2$$。
所以$$f(a, b, c, n) = f(a mod c, b mod c, c, n) + frac{n(n + 1)}{2} imes lfloor frac{a}{c}
floor + (n + 1) imes lfloor frac{b}{c}
floor$$。
所以只需考虑$a < c$且$b < c$的情况。
令$m = lfloor frac{an + b}{c}
floor$,
$sumlimits_{i = 0}^n lfloor frac{ai + b}{c}
floor$
$= sumlimits_{i = 0}^n sumlimits_{j = 1}^m [lfloor frac{ai + b}{c}
floor ge j]$
$= sumlimits_{i = 0}^n sumlimits_{j = 0}^{m - 1} [lfloor frac{ai + b}{c}
floor ge j + 1]$
$= sumlimits_{i = 0}^n sumlimits_{j = 0}^{m - 1} [ai ge jc + c - b]$
$= sumlimits_{i = 0}^n sumlimits_{j = 0}^{m - 1} [ai > jc + c - b - 1]$
$= sumlimits_{i = 0}^n sumlimits_{j = 0}^{m - 1} [i > frac{cj + c - b - 1}{a}]$
$= sumlimits_{j = 0}^{m - 1} sumlimits_{i = 0}^n [i > frac{cj + c - b - 1}{a}]$
$= sumlimits_{j = 0}^{m - 1} (n - lfloor frac{cj + c - b - 1}{a}
floor)$
$= nm - sumlimits_{j = 0}^{m - 1} lfloor frac{cj + c - b - 1}{a}
floor$
$= nm - f(c, c - b - 1, a, m - 1)$
综上:
$$f(a, b, c, n) = left { egin{aligned} & {(n + 1) imes lfloor frac{b}{c}
floor} & a = 0 & \ & {-f(c, c - b mod c - 1, a mod c, lfloor frac{(a mod c)n + b mod c}{c}
floor - 1) + n imes lfloor frac{(a mod c)n + b mod c}{c}
floor + frac{n(n + 1)}{2} imes lfloor frac{a}{c}
floor + (n + 1) imes lfloor frac{b}{c}
floor} & a > 0 & end{aligned}
ight.]
它把求(f(a, b, c, n))转化为求(f(c, b', a mod c, n')),这和欧几里得算法很类似,这类算法称之为类欧几里得算法。
留坑......