类欧几里得算法
手推不难。
求
\[f(a,b,c,n)=\sum_{i=0}^{n}\lfloor\frac{ai+b}{c}\rfloor
\]
\[g(a,b,c,n)=\sum_{i=0}^{n}\lfloor\frac{ai+b}{c}\rfloor^2
\]
\[h(a,b,c,n)=\sum_{i=0}^{n}i\lfloor\frac{ai+b}{c}\rfloor
\]
- \(a=0\)
\[f(a,b,c,n)=(n+1)\lfloor\frac{b}{c}\rfloor
\]
\[g(a,b,c,n)=(n+1)\lfloor\frac{b}{c}\rfloor^2
\]
\[h(a,b,c,n)=\frac{n(n+1)}{2}\lfloor\frac{b}{c}\rfloor
\]
- \(a\ge c\space\text{or}\space b\ge c\)
\[f(a,b,c,n)=\frac{n(n+1)}{2}\lfloor\frac{a}{c}\rfloor+(n+1)\lfloor\frac{b}{c}\rfloor+f(a\space\text{mod}\space c,b\space\text{mod}\space c,c,n)
\]
\[g(a,b,c,n)=g(a\space\text{mod}\space c,b\space\text{mod}\space c,c,n)+2\lfloor\frac{a}{c}\rfloor h(a\space\text{mod}\space c,b\space\text{mod}\space c,c,n)+2\lfloor\frac{b}{c}\rfloor f(a\space\text{mod}\space c,b\space\text{mod}\space c,c,n)
\]
\[+\frac{n(n+1)(2n+1)}{6}\lfloor\frac{a}{c}\rfloor^2+n(n+1)\lfloor\frac{a}{c}\rfloor\lfloor\frac{b}{c}\rfloor+(n+1)\lfloor\frac{b}{c}\rfloor^2
\]
\[h(a,b,c,n)=h(a\space\text{mod}\space c,b\space\text{mod}\space c,c,n)+\frac{n(n+1)(2n+1)}{6}\lfloor\frac{a}{c}\rfloor+\frac{n(n+1)}{2}\lfloor\frac{b}{c}\rfloor
\]
- \(a<c\space\text{and}\space b<c\)
记 \(M=\lfloor\frac{an+b}{c}\rfloor\) .
\[f(a,b,c,n)=nM-f(c,c-b-1,a,M-1)
\]
\[g(a,b,c,n)=nM(M+1)-f(a,b,c,n)-2h(c,c-b-1,a,M-1)-2f(c,c-b-1,a,M-1)
\]
\[h(a,b,c,n)=\frac{1}{2}\lbrack n(n+1)M-g(c,c-b-1,a,M-1)-f(c,c-b-1,a,M-1)\rbrack
\]
最后一档用贡献法。同步计算时间复杂度 \(O(log\space n)\) .