类欧几里得算法学习笔记
如何求\(\sum_{i=0}^ni^{k1}\lfloor\frac{ai+b}{c}\rfloor^{k2}\)?\((1\leqslant n,a,b,c\leqslant10^9,k1+k2\leqslant 10)\)
设\(f(n,a,b,c)[k1][k2]\)表示原式的值,则:
若\(a\geqslant c\),设\(a=pc+q(0\leqslant q<c)\),则
\[\begin{align}
&\;\;\;\;\;f(n,a,b,c)[k1][k2]\\
&=\sum_{i=0}^ni^{k1}\lfloor\frac{ai+b}{c}\rfloor^{k2}\\
&=\sum_{i=0}^ni^{k1}(pi+\lfloor\frac{qi+b}{c}\rfloor)^{k2}\\
&=\sum_{i=0}^ni^{k1}\sum_{j=0}^{k2}\text{C}_{k2}^jp^ji^j\lfloor\frac{qi+b}{c}\rfloor^{k2-j}\\
&=\sum_{j=0}^{k2}\text{C}_{k2}^jp^j\sum_{i=0}^ni^{k1+j}\lfloor\frac{qi+b}{c}\rfloor^{k2-j}\\
&=\sum_{j=0}^{k2}\text{C}_{k2}^jp^jf(n,q,b,c)[k1+j][k2-j]
\end{align}\]
只需递归计算\(f(n,q,b,c)\)并\(O(k^3)\)处理即可。
若\(b\geqslant c\),设\(b=pc+q(0\leqslant q<c)\),则
\[\begin{align}
&\;\;\;\;\;f(n,a,b,c)[k1][k2]\\
&=\sum_{i=0}^ni^{k1}\lfloor\frac{ai+b}{c}\rfloor^{k2}\\
&=\sum_{i=0}^ni^{k1}(p+\lfloor\frac{ai+q}{c}\rfloor)^{k2}\\
&=\sum_{i=0}^ni^{k1}\sum_{j=0}^{k2}\text{C}_{k2}^jp^j\lfloor\frac{ai+q}{c}\rfloor^{k2-j}\\
&=\sum_{j=0}^{k2}\text{C}_{k2}^jp^j\sum_{i=0}^ni^{k1}\lfloor\frac{ai+q}{c}\rfloor^{k2-j}\\
&=\sum_{j=0}^{k2}\text{C}_{k2}^jp^jf(n,a,q,c)[k1][k2-j]
\end{align}\]
只需递归计算\(f(n,a,q,c)\)并\(O(k^3)\)处理即可。
若\(c>a\)且\(c>b\),则有:
\[\begin{align}
&\;\;\;\;\;f(n,a,b,c)[k1][k2]\\
&=\sum_{i=0}^ni^{k1}\sum_{j=0}^{\lfloor\frac{ai+b}{c}\rfloor-1}((j+1)^{k2}-[j\neq0]j^{k2})\\
&=\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}((j+1)^{k2}-[j\neq0]j^{k2})\sum_{i=0}^n[j<\lfloor\frac{ai+b}{c}\rfloor]i^{k1}\\
\end{align}\]
又
\[\begin{align}
&\;\;\;\;\;j<\lfloor\frac{ai+b}{c}\rfloor\\
&\Leftrightarrow j+1\leqslant\lfloor\frac{ai+b}{c}\rfloor\\
&\Leftrightarrow jc+c\leqslant ai+b\\
&\Leftrightarrow jc+c-b-1<ai\\
&\Leftrightarrow \lfloor\frac{jc+c-b-1}{a}\rfloor<i
\end{align}\]
于是,设\(T_{i,j}\)为\(\sum_{w=0}^{x}w^i\)的多项式表达式的\(j\)次项系数
\[\begin{align}
&\;\;\;\;\;f(n,a,b,c)[k1][k2]\\
&=\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}((j+1)^{k2}-[j\neq0]j^{k2})\sum_{i=0}^n[i>\lfloor\frac{jc+c-b-1}{a}\rfloor]i^{k1}\\
&=\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}((j+1)^{k2}-[j\neq0]j^{k2})(\sum_{i=0}^ni^{k1}-\sum_{i=0}^{\lfloor\frac{jc+c-b-1}{a}\rfloor}i^{k1})\\
&=\lfloor\frac{an+b}{c}\rfloor^{k2}\sum_{i=0}^ni^{k1}-\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}\sum_{p=0}^{k2-1}\sum_{q=0}^{k1+1}\text{C}_{k2}^{p}T_{k1,q}j^p\lfloor\frac{jc+c-b-1}{a}\rfloor^q\\
&=\lfloor\frac{an+b}{c}\rfloor^{k2}\sum_{i=0}^ni^{k1}-\sum_{p=0}^{k2-1}\sum_{q=0}^{k1+1}\text{C}_{k2}^{p}T_{k1,q}\sum_{j=0}^{\lfloor\frac{an+b}{c}\rfloor-1}j^p\lfloor\frac{jc+c-b-1}{a}\rfloor^q\\
&=\lfloor\frac{an+b}{c}\rfloor^{k2}\sum_{i=0}^ni^{k1}-\sum_{p=0}^{k2-1}\sum_{q=0}^{k1+1}\text{C}_{k2}^{p}T_{k1,q}f(\lfloor\frac{an+b}{c}\rfloor-1,c,c-b-1,a)[p][q]
\end{align}\]
只需递归计算\(f(\lfloor\frac{an+b}{c}\rfloor-1,c,c-b-1,a)\)并\(O(k^4)\)处理即可。
当\(a=0\)或\(n\)很小时可直接计算。
Please not contact lydsy2012@163.com!