类欧几里得算法因为形式上像欧几里得算法而得名,其时间复杂度也和欧几里得算法一致,类欧几里得算法有很多应用,下面只举部分例子。
目标
求出以下三个函数值
f(a,b,c,n)=∑ni=0⌊ai+bc⌋
g(a,b,c,n)=∑ni=0i⌊ai+bc⌋
h(a,b,c,n)=∑ni=0⌊ai+bc⌋2
设 m=⌊an+bc⌋
f
当a==0时
f(a,b,c,n)=∑ni=0⌊ai+bc⌋
=bc(n+1)
当(a>=c)||(b>=c)时
f(a,b,c,n)=∑ni=0⌊ai+bc⌋
=∑ni=0(aci+bc+(a%c)i+b%cc)
=acn(n+1)/2+bc(n+1)+f(a%c,b%c,c,n)
当 (a<c)and(b<c) 时
f(a,b,c,n)=∑ni=0⌊ai+bc⌋
=∑ni=0∑mj=1[ai+bc>=j]
=∑ni=0∑m−1j=0[ai+bc>=j+1]
=∑ni=0∑m−1j=0[ai+b>=cj+c]
=∑ni=0∑m−1j=0[ai+b>cj+c−1]
=∑ni=0∑m−1j=0[i>cj+c−b−1a]
=∑m−1j=0∑ni=0[i>cj+c−b−1a]
=∑m−1j=0(n−cj+c−b−1a)
=nm−∑m−1j=0cj+c−b−1a
=nm−f(c,c−b−1,a,m−1)
g
当a==0时
g(a,b,c,n)=∑ni=0i⌊ai+bc⌋
=⌊bc⌋n∗(n+1)/2
当(a>=c)||(b>=c)时
g(a,b,c,n)=∑ni=0i⌊ai+bc⌋
=∑ni=0i(aci+bc+(a%c)i+b%cc)
=acn(n+1)(2n+1)/6+bcn(n+1)/2+g(a%c,b%c,c,n)
当 (a<c)and(b<c)
g(a,b,c,n)=∑ni=0i⌊ai+bc⌋
=∑m−1j=0∑ni=0i[i>cj+c−b−1a]
=∑m−1j=0(n+cj+c−b−1a−1)(n−cj+c−b−1a)/2
=12mn(n+1)−12f(c,c−b−1,a,m−1)−12h(c,c−b−1,a,m−1)
h
当a==0时
h(a,b,c,n)=∑ni=0⌊ai+bc⌋2
=(bc)2(n+1)
当(a>=c)||(b>=c)时
h(a,b,c,n)=∑ni=0⌊ai+bc⌋2
x2=2x(x+1)2−x
=2∑xi=1i−x
h(a,b,c,n)=∑ni=0⌊ai+bc⌋2
=2∑ni=0∑ai+bcj=1j−f(a,b,c,n)
=2∑mi=1i∑nj=0[aj+bc>=i]−f(a,b,c,n)
=2∑m−1i=0(i+1)∑nj=0[j>ci+c−b−1a]−f(a,b,c,n)
=nm(m+1)−2f(c,c−b−1,a,m−1)−2g(c,c−b−1,a,m−1)−f(a,b,c,n)
至此我们已经可以解决这一类类欧几里得问题。