类欧几里得算法之所以得名是因为其算法复杂度证明与扩展欧几里得算法类似。
我认为类欧更偏向于是一种思想。
其主要思想就是寻找可以简便计算的边界,然后通过化式子将不同情况化为边界递归计算。
通过几个具体的例子,可以更好地理解其思想。
P5170 【模板】类欧几里得算法
推导
\[ 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 \]
\[ f(a,b,c,N)=\begin{cases}(N+1)\lfloor\frac{b}{c}\rfloor&a=0\\\frac{N(N+1)}{2}\lfloor\frac{a}{c}\rfloor+(N+1)\lfloor\frac{b}{c}\rfloor+f(a\bmod c,b\bmod c,c,N)&a\ge c\ or\ b\ge c\\NM-f(c,c-b-1,a,M-1),M=\lfloor\frac{aN+b}{c}\rfloor &otherwise\end{cases} \]
\[ g(a,b,c,N)=\begin{cases}(N+1)\lfloor\frac{b}{c}\rfloor^2&a=0\\g(a\bmod c,b\bmod c,c,N)+2\lfloor\frac{a}{c}\rfloor h(a\bmod c,b\bmod c,c,N)+2\lfloor\frac{b}{c}\rfloor f(a\bmod c,b\bmod 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&a\ge c\ or\ b\ge c\\NM(M+1)-f(a,b,c,N)-2h(c,c-b-1,a,M-1)-2f(c,c-b-1,a,M-1)&otherwise\end{cases} \]
\[ h(a,b,c,N)=\begin{cases}\frac{N(N+1)}{2}\lfloor\frac{b}{c}\rfloor&a=0\\h(a\bmod c,b\bmod 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\ge c\ or\ b\ge c\\\frac{1}{2}[MN(N+1)-g(c,c-b-1,a,M-1)-f(c,c-b-1,a,M-1)]&otherwise\end{cases} \]
代码
1 |
|
P5171 Earthquake
题意
求满足 \(ax+by⩽c\) 的非负整数解的个数。
推导
\[ y⩽\left\lfloor\frac{c-ax}{b}\right\rfloor\\ ans=\sum_{x=0}^{\left\lfloor\frac{c}{a}\right\rfloor}\left\lfloor\frac{c-ax}{b}\right\rfloor+1\\ =\sum_{x=0}^{\left\lfloor\frac{c}{a}\right\rfloor}\left\lfloor\frac{c+(b-a)x}{b}\right\rfloor-x+1\\ (\sum_{x=0}^{\left\lfloor\frac{c}{a}\right\rfloor}\left\lfloor\frac{c+(b-a)x}{b}\right\rfloor)-\frac{\left\lfloor\frac{c}{a}\right\rfloor(\left\lfloor\frac{c}{a}\right\rfloor+1)}{2}+\left\lfloor\frac{c}{a}\right\rfloor+1 \]
代码
1 |
|
P5172 Sum
推导
\[ ans=\sum_{d=1}^n(-1)^{\lfloor d\sqrt r\rfloor}\\=\sum_{d=1}^n1-2(\lfloor d\sqrt r\rfloor\bmod 2)\\=\sum_{d=1}^n1-2(\lfloor d\sqrt r\rfloor-2\lfloor \frac{d\sqrt r}2\rfloor) \]
\[ f(a,b,c,n)=\sum_{d=1}^n\lfloor\frac{a\sqrt r+b}{c}d\rfloor \]
当 \(\lfloor\frac{a\sqrt r+b}{c}\rfloor\ge1\) 时, \[ f(a,b,c,n)=\sum_{d=1}^n\lfloor\frac{a\sqrt r+b}{c}d\rfloor\\=\sum_{d=1}^n\lfloor\frac{a\sqrt r+b-c\lfloor\frac{a\sqrt r+b}{c}\rfloor}{c}d+\lfloor\frac{a\sqrt r+b}{c}\rfloor d\rfloor\\=\sum_{d=1}^nf(a,b-c\lfloor\frac{a\sqrt r+b}{c}\rfloor,c,n)+\frac 1 2n(n+1)\lfloor\frac{a\sqrt r+b}{c}\rfloor \] 当 \(\lfloor\frac{a\sqrt r+b}{c}\rfloor=0\) 时, \[ f(a,b,c,n)=\sum_{d=1}^n\lfloor\frac{a\sqrt r+b}{c}d\rfloor\\=\sum_{d=1}^n\sum_{p=1}^{\lfloor\frac{a\sqrt r+b}{c}n\rfloor}[p\le\frac{a\sqrt r+b}{c}d]\\=\sum_{d=1}^n\sum_{p=1}^{\lfloor\frac{a\sqrt r+b}{c}n\rfloor}[d\ge\frac{cp}{a\sqrt r+b}]\\=\sum_{p=1}^{\lfloor\frac{a\sqrt r+b}{c}n\rfloor} n-\lfloor\frac{cp}{a\sqrt r+b}\rfloor\\\texttt{(无理数,大于等于与大于等价)}\\=\lfloor\frac{a\sqrt r+b}{c}n\rfloor n-\sum_{p=1}^{\lfloor\frac{a\sqrt r+b}{c}n\rfloor}\lfloor\frac{c(a\sqrt r -b)}{a^2r-b^2}p\rfloor\\=\lfloor\frac{a\sqrt r+b}{c}n\rfloor n-f(ac,-bc,a^2r-b^2,\lfloor\frac{a\sqrt r+b}{c}n\rfloor) \]
\[ ans=1-2f(1,0,1,n)+4f(1,0,2,n) \]
代码
1 |
|
P5179 Fraction
推导
\[ f(a,b,p,q,c,d)\begin{cases}q=1,p=\left\lfloor\frac{a}{c}\right\rfloor+1&\left\lfloor\frac{a}{c}\right\rfloor+1⩽\left\lceil\frac{c}{d}\right\rceil-1\\p=1,q=\left\lfloor\frac{d}{c}\right\rfloor+1&a=0\\ f(b,a,q,p,d,c)&a⩽b \and c⩽d\\f(a\mod b,b,p,q,c-\left\lfloor\frac{a}{b}\right\rfloor d,d),p=p+\left\lfloor\frac{a}{b}\right\rfloor q&a\ge b\end{cases} \]
代码
1 |
|