类欧几里得算法

类欧几里得算法

基础

\[f(a,b,c,n)=\sum_{i=0}^n \left\lfloor \dfrac{ai+b}{c}\right\rfloor \]

\(a\ge c\)\(b\ge c\)

\[\begin{aligned} f(a,b,c,n)&=\sum_{i=0}^n \left\lfloor \dfrac{ai+b}{c}\right\rfloor\\ &=\sum_{i=0}^n \left\lfloor \dfrac{c\left\lfloor\frac{a}{c}\right\rfloor i+a\bmod c\times i+c\left\lfloor\frac{b}{c}\right\rfloor+b\bmod c}{c}\right\rfloor\\ &=\sum_{i=0}^n\left\lfloor\dfrac{a}{c}\right\rfloor i+\left\lfloor\frac{b}{c}\right\rfloor+\left\lfloor \dfrac{a\bmod c\times i+b\bmod c}{c}\right\rfloor\\ &=\left\lfloor\dfrac{a}{c}\right\rfloor\dfrac{n(n+1)}{2}+\left\lfloor\dfrac{b}{c}\right\rfloor(n+1)+f(a\bmod c,b\bmod c,c,n) \end{aligned} \]

\(a<c\)\(b<c\)

\(\left\lfloor \dfrac{ai+b}{c}\right\rfloor\)替换为\(\sum\limits_{j=0}\left[ j<\left\lfloor \dfrac{ai+b}{c}\right\rfloor\right]\),其中\(j\)的上界为\(\left\lfloor \dfrac{an+b}{c}\right\rfloor-1\)。也就是求

\[f(a,b,c,n)=\sum_{i=0}^n\sum_{j=0}\left[j<\left\lfloor \dfrac{ai+b}{c}\right\rfloor\right] \]

交换求和号

\[f(a,b,c,d)=\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}\sum_{i=0}^n\left[j<\left\lfloor\dfrac{ai+b}{c}\right\rfloor\right]\\ \]

乱搞一下后面那个东西,通过下取整与不等号的变换把\(i\)搞掉

\[j<\left\lfloor\dfrac{ai+b}{c}\right\rfloor \Leftrightarrow j+1\le \left\lfloor\dfrac{ai+b}{c}\right\rfloor\Leftrightarrow j+1\le \dfrac{ai+b}{c}\\ \Leftrightarrow \dfrac{cj+c-b}{a}\le i \Leftrightarrow i\ge \dfrac{cj+c-b}{a}\Leftrightarrow i>\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor \]

\(i\)的上界是确定的,为\(n\),也就是说\(j<\left\lfloor\dfrac{ai+b}{c}\right\rfloor\)\(i\)的个数等于\(n-\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor\)

\[\begin{aligned} f(a,b,c,d)&=\sum_{j=0}^{\lfloor \frac{an+b }{c} \rfloor-1}\sum_{i=0}^n\left[j<\left\lfloor\dfrac{ai+b}{c}\right\rfloor\right]\\ &=\sum_{j=0}^{\lfloor \frac{an+b }{c} \rfloor-1}n-\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor\\ &= \left\lfloor\dfrac{an+b}{c}\right\rfloor n-\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor\\ &= \left\lfloor\dfrac{an+b}{c}\right\rfloor n-f(c,c-b-1,a,\lfloor\frac{an+b}{c}\rfloor -1) \end{aligned} \]

以后方便起见记\(\left\lfloor\dfrac{an+b}{c}\right\rfloor=m\),记\(\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor=t\),即

\[f(a,b,c,n)=mn-f(c,c-b-1,a,m-1) \]

递归求解即可,边界为\(a=0\)

复杂度\(O(\log n)\),证明类似欧几里得算法,故算法得名

扩展

\[g(a,b,c,n)=\sum_{i=0}^n i\left\lfloor \dfrac{ai+b}{c}\right\rfloor\\ h(a,b,c,n)=\sum_{i=0}^n \left\lfloor \dfrac{ai+b}{c}\right\rfloor^2 \]

\(g(a,b,c,n)\)

\(a\ge c\)\(b\ge c\)

\[\begin{aligned} g(a,b,c,n)&=\sum_{i=0}^n i\left\lfloor \dfrac{ai+b}{c}\right\rfloor\\ &=\sum_{i=0}^n i\left\lfloor \dfrac{c\left\lfloor\frac{a}{c}\right\rfloor i+a\bmod c\times i+c\left\lfloor\frac{b}{c}\right\rfloor+b\bmod c}{c}\right\rfloor\\ &=\sum_{i=0}^n\left\lfloor\dfrac{a}{c}\right\rfloor i^2+\left\lfloor\frac{b}{c}\right\rfloor i+\left\lfloor \dfrac{a\bmod c\times i+b\bmod c}{c}\right\rfloor i\\ &=\left\lfloor\dfrac{a}{c}\right\rfloor\dfrac{n(n+1)(2n+1)}{6}+\left\lfloor\dfrac{b}{c}\right\rfloor\dfrac{n(n+1)}{2}+g(a\% c,b\% c,c,n) \end{aligned} \]

\(a<c\)\(b<c\)

\(m=\left\lfloor\dfrac{an+b}{c}\right\rfloor\)\(t=\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor\)

\[\begin{aligned} g(a,b,c,n)&=\sum_{i=0}^ni\sum_{j=0}\left[j<\left\lfloor \dfrac{ai+b}{c}\right\rfloor\right]\\ &=\sum_{j=0}^{m-1}\sum_{i=0}^ni\left [j<\left\lfloor\dfrac{ai+b}{c}\right\rfloor\right]\\ &=\sum_{j=0}^{m-1}\sum_{i=0}^ni [i>t]\\ &=\sum_{j=0}^{m-1}\frac{n(n+1)}{2}-\frac{t(t+1)}{2}\\ &=\dfrac{1}{2}[mn(n+1)-\sum_{j=0}^{m-1}t^2-\sum_{j=0}^{m-1}t\ ]\\ &=\dfrac{1}{2}[mn(n+1)-h(c,c-b-1,a,m-1)-f(c,c-b-1,a,m-1)\ ] \end{aligned} \]

递归边界:\(a=0\)

\(h(a,b,c,n)\)

\(a\ge c\)\(b\ge c\)

\[\begin{aligned} h(a,b,c,n)&=\sum_{i=0}^n \left\lfloor \dfrac{ai+b}{c}\right\rfloor^2\\ &=\sum_{i=0}^n\left(\left\lfloor\dfrac{a}{c}\right\rfloor i+\left\lfloor\frac{b}{c}\right\rfloor +\left\lfloor \dfrac{a\bmod c\times i+b\bmod c}{c}\right\rfloor\right)^2 \\ &=\sum_{i=0}^n\left\lfloor\dfrac{a}{c}\right\rfloor^2 i^2+\left\lfloor\frac{b}{c}\right\rfloor^2 +\left\lfloor \dfrac{a \% c\times i+b\% c}{c}\right\rfloor^2+2\left\lfloor\dfrac{a}{c}\right\rfloor\left\lfloor\frac{b}{c}\right\rfloor i\\&\ \ \ \ \ +2\left\lfloor\dfrac{a}{c}\right\rfloor\left\lfloor \dfrac{a\% c\times i+b\% c}{c}\right\rfloor i+2\left\lfloor\dfrac{b}{c}\right\rfloor\left\lfloor \dfrac{a\% c\times i+b\% c}{c}\right\rfloor\\ &=\frac{n(n+1)(2n+1)}{6}\left\lfloor\dfrac{a}{c}\right\rfloor^2+(n+1)\left\lfloor\frac{b}{c}\right\rfloor^2+n(n+1)\left\lfloor\dfrac{a}{c}\right\rfloor\left\lfloor\frac{b}{c}\right\rfloor+2\left\lfloor\dfrac{a}{c}\right\rfloor g(a\% c,b\%c,c,n)\\ &\ \ \ \ \ +2\left\lfloor\dfrac{b}{c}\right\rfloor f(a\%c,b\%c,c,n)+h(a\%c,b\%c,c,n) \end{aligned} \]

\(a<c\)\(b<c\)

\(m=\left\lfloor\dfrac{an+b}{c}\right\rfloor\)\(t=\left\lfloor\dfrac{cj+c-b-1}{a}\right\rfloor\)

\[h(a,b,c,n)=\sum_{i=0}^n \left\lfloor \dfrac{ai+b}{c}\right\rfloor^2 \]

\(n^2\)写成\(\left(2\sum\limits_{i=1}^n i\right)-n\)

\[\begin{aligned} h(a,b,c,n)&=\sum_{i=0}^n\left(2\sum_{j=1}^{\left\lfloor \frac{ai+b}{c}\right\rfloor}j\right)-\left\lfloor \dfrac{ai+b}{c}\right\rfloor\\ &=\left(2\sum_{i=0}^n\sum_{j=0}^{\left\lfloor \frac{ai+b}{c}\right\rfloor-1}j+1\right)-f(a,b,c,n)\\ &=\left(2\sum_{j=0}^{m-1}\sum_{i=0}^n (j+1)\left[j< \left\lfloor \frac{ai+b}{c}\right\rfloor\right]\right)-f(a,b,c,n) \end{aligned} \]

其中

\[\begin{aligned} \sum_{j=0}^{m-1}\sum_{i=0}^n (j+1)\left[j< \left\lfloor \frac{ai+b}{c}\right\rfloor\right]&=\sum_{j=1}^{m-1}(j+1)\sum_{i=0}^n \left[j< \left\lfloor \frac{ai+b}{c}\right\rfloor\right]\\ &=\sum_{j=0}^{m-1}(j+1)\sum_{i=0}^n[i>t]\\ &=\sum_{j=0}^{m-1}(j+1)(n-t)\\ &=\dfrac{m(m+1)}{2}n-f(c,c-b-1,a,m-1) -g(c,c-b-1,a,m-1) \end{aligned} \]

所以

\[\begin{aligned} h(a,b,c,n)&=m(m+1)n-2f(c,c-b-1,a,m-1)-2g(c,c-b-1,a,m-1)-f(a,b,c,n) \end{aligned} \]

递归边界:\(a=0\)

实现注意事项

因为\(g,h\)有相互调用,递归时把一组\((a,b,c,n)\)\(f,g,h\)都求出来

posted @ 2021-05-11 22:48  harryzhr  阅读(71)  评论(0编辑  收藏  举报