类欧几里得算法
学习背景
之前做了一道类欧+\(Stern-Brocot\ Tree\)二分的题目,就学一下。
问题模型
求
\[\begin{aligned}
f(n,a,b,c) &= \sum_{i=0}^{n} \left\lfloor \frac{ai+b}{c} \right\rfloor \\
g(n,a,b,c) &= \sum_{i=0}^{n} i \left\lfloor \frac{ai+b}{c} \right\rfloor \\
h(n,a,b,c) &= \sum_{i=0}^{n} \left(\left\lfloor \frac{ai+b}{c} \right\rfloor\right)^2
\end{aligned}
\]
解法推导
为了方便,我们设\(m= \left\lfloor \frac{na+b}{c} \right\rfloor\)
注意,因为取模符号太宽了,所以用C++的"%"来表示取模。
(一)\(f(n,a,b,c)\)
- 第一种情况:\(a<c\)且\(b<c\)
这个可以直接推导,比较简单。
\[\begin{aligned}
f(n,a,b,c) &= \sum_{i=0}^{n} \left\lfloor \frac{ai+b}{c} \right\rfloor \\
&= \sum_{i=0}^{n} \sum_{j=0}^{n}
\left[j < \left\lfloor \frac{ai+b}{c}\right\rfloor \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
\left[j < \left\lfloor \frac{ai+b}{c}\right\rfloor \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
\left[j < \left\lceil \frac{ai+b-c+1}{c}\right\rceil \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
\left[j < \frac{ai+b-c+1}{c} \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
\left [cj < ai+b-c+1 \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
\left[ i> \frac{cj+c-b-1}{a} \right] \\
&= \sum_{j=0}^{m-1} n - \left\lfloor \frac{cj+c-b-1}{a} \right\rfloor \\
&= nm - \sum_{j=0}^{m-1} \left\lfloor \frac{cj+c-b-1}{a} \right\rfloor \\
&= nm - f(m - 1, c, c - b - 1, a)
\end{aligned}
\]
- 第二种情况:\(a\geq c\)且\(b\geq c\)
这时候我们只要将\(\left\lfloor \frac{ai+b}{c} \right\rfloor\)拆开,就是:
\[\begin{aligned}
f(n,a,b,c) &= \sum_{i=0}^{n} \left\lfloor \frac{ai+b}{c} \right\rfloor \\
&= \sum_{i=0}^{n}
\left(
\left\lfloor \frac{(a \% c)i+(b \% c)}{c} \right\rfloor +
i \left\lfloor \frac{a}{c} \right \rfloor +
\left\lfloor \frac{b}{c} \right\rfloor
\right) \\
&= \frac{n(n+1)}{2} \left\lfloor \frac{a}{c} \right\rfloor +
n \left\lfloor \frac{b}{c} \right\rfloor +
\sum_{i=0}^{n}
\left\lfloor \frac{(a \% c)i+(b \% c)}{c} \right\rfloor
\end{aligned}
\]
(二)\(g(n,a,b,c)\)
第二类和第一类比较类似,就写得简单一点。
- 第一种情况:\(a<c\)且\(b<c\)
\[\begin{aligned}
g(n,a,b,c) &= \sum_{i=0}^{n} i \left\lfloor \frac{ai+b}{c} \right\rfloor \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
i \left[ \left\lfloor\ \frac{ai+b}{c} \right\rfloor > j \right] \\
&= \sum_{j=0}^{m-1} \sum_{i=0}^{n}
i \left[ i > \left\lfloor \frac{cj+c-b-1}{a} \right\rfloor \right] \\
&= \frac{mn(n+1)}{2} -
\sum_{j=0}^{m-1}
\frac{\left\lfloor \frac{cj+c-b-1}{a} \right\rfloor
(\left\lfloor \frac{cj+c-b-1}{a} \right\rfloor+1)}{2} \\
&= \frac{mn(n+1)}{2} -
\frac{1}{2} \sum_{j=0}^{m-1}
\left( \left\lfloor \frac{cj+c-b-1}{a} \right\rfloor \right)^2 -
\frac{1}{2} \sum_{j=0}^{m-1}
\left\lfloor \frac{cj+c-b-1}{a} \right\rfloor \\
&= \frac{mn(n+1)}{2} - \frac{1}{2}h(n,c,c-b-1,a) - \frac{1}{2}f(n,c,c-b-1,a)
\end{aligned}
\]
- 第二种情况:\(a\geq c\)且\(b\geq c\)
这个也和\(f(n,a,b,c)\)类似,直接给出结论了:
\[ g(n,a,b,c) = \frac{n(n+1)(2n+1)}{6}\left\lfloor \frac{a}{c} \right\rfloor +
\frac{n(n+1)}{2}\left\lfloor \frac{b}{c} \right\rfloor +
g(n,a \% c, b \% c, c)
\]
(三)\(h(n,a,b,c)\)
因为这里涉及到了平方,所以我们考虑用一种求和式表示平方。
\[ n^2 = 2\sum_{i=0}^{n} i - n
\]
之后,有如下推导:
- 第一种情况:\(a<c\)且\(b<c\)
\[ h(n,a,b,c) = \sum_{i=0}^{n} \left( 2\sum_{j=0}^{\left \lfloor\frac{ai+b}{c}\right\rfloor}j - \left\lfloor\frac{ai+b}{c}\right\rfloor \right)
\]
之后,就用与求\(f\)和\(g\)类似的方法,得到:
\[ h(n,a,b,c) = nm(m+1) - 2g(m-1,c,c-b-1,a) - 2f(m-1,c,c-b-1,a) - f(n,a,b,c)
\]
- 第二种情况:\(a\geq c\)且\(b\geq c\)
对于这个,就直接将三项式暴力拆开,就有:
\[\begin{aligned}
h(n,a,b,c) = \left\lfloor \frac{a}{c} \right\rfloor^2 \times \frac{n(n+1)(2n+1)}{6} +
\left\lfloor \frac{b}{c} \right\rfloor^2 \times (n+1) +
\left\lfloor \frac{a}{c} \right\rfloor \times \left\lfloor \frac{b}{c} \right\rfloor \times n(n+1) +
h(n,a \% c, b \% c, c) +
2\left\lfloor \frac{a}{c} \right\rfloor g(n,a \% c, b \% c, c) +
2\left\lfloor \frac{b}{c} \right\rfloor f(n,a \% c, b \% c, c)
\end{aligned}
\]
(四)整理一下
\[f(n,a,b,c) =
\begin{cases}
\frac{n(n+1)}{2} \left\lfloor \frac{a}{c} \right\rfloor + n \left\lfloor \frac{b}{c} \right\rfloor + f(m-1, a \% c, b \% c, c) & a < c \And b < c \\
nm - f(m - 1, c, c - b - 1, a) & otherwise
\end{cases}
\]
\[g(n,a,b,c) =
\begin{cases}
\frac{mn(n+1)}{2} - \frac{1}{2}h(n,c,c-b-1,a) - \frac{1}{2}f(n,c,c-b-1,a) & a < c \And b < c \\
\frac{n(n+1)(2n+1)}{6}\left\lfloor \frac{a}{c} \right\rfloor +
\frac{n(n+1)}{2}\left\lfloor \frac{b}{c} \right\rfloor +
g(n,a \% c, b \% c, c) & otherwise
\end{cases}
\]
\[h(n,a,b,c) =
\begin{cases}
h(n,a,b,c) = nm(m+1) - 2g(m-1,c,c-b-1,a) - 2f(m-1,c,c-b-1,a) - f(n,a,b,c)
& a < c \And b < c \\
\left\lfloor \frac{a}{c} \right\rfloor^2 \times \frac{n(n+1)(2n+1)}{6} +
\left\lfloor \frac{b}{c} \right\rfloor^2 \times (n+1) +
\left\lfloor \frac{a}{c} \right\rfloor \times \left\lfloor \frac{b}{c} \right\rfloor \times n(n+1) +
h(n,a \% c, b \% c, c) +
2\left\lfloor \frac{a}{c} \right\rfloor g(n,a \% c, b \% c, c) +
2\left\lfloor \frac{b}{c} \right\rfloor f(n,a \% c, b \% c, c) & otherwaise
\end{cases}
\]