类欧几里得算法学习

文章目录

  • 几个常用等式
  • 类欧几里得
    • 介绍
    • 求f
    • 求g
    • 求h

几个常用等式

  • a ≤ ⌊ b c ⌋ ⇔ a c ≤ b a\le\left\lfloor\frac bc\right\rfloor\Leftrightarrow ac\le b acbacb
  • a < ⌈ b c ⌉ ⇔ a c < b a<\left\lceil\frac bc\right\rceil\Leftrightarrow ac<b a<cbac<b
  • a ≥ ⌈ b c ⌉ ⇔ a c ≥ b a\ge\left\lceil\frac bc\right\rceil\Leftrightarrow ac\ge b acbacb
  • a > ⌊ b c ⌋ ⇔ a c > b a>\left\lfloor\frac bc\right\rfloor\Leftrightarrow ac>b a>cbac>b
  • ⌊ b c ⌋ ⇔ ⌊ b + c − 1 c ⌋ \left\lfloor\frac bc\right\rfloor\Leftrightarrow \left\lfloor\frac {b+c-1}c\right\rfloor cbcb+c1
  • ⌊ b c ⌋ ⇔ ⌈ b − c + 1 c ⌉ \left\lfloor\frac bc\right\rfloor\Leftrightarrow \left\lceil\frac {b-c+1}c\right\rceil cbcbc+1

类欧几里得

介绍

可以用来快速求出以下几个式子的值:

  • f ( a , b , c , n ) = ∑ i = 0 n ⌊ a i + b c ⌋ f(a,b,c,n)=\sum_{i=0}^n\left\lfloor\frac{ai+b}c\right\rfloor f(a,b,c,n)=i=0ncai+b
  • g ( a , b , c , n ) = ∑ i = 0 n i ⌊ a i + b c ⌋ g(a,b,c,n)=\sum_{i=0}^ni\left\lfloor\frac{ai+b}c\right\rfloor g(a,b,c,n)=i=0nicai+b
  • h ( a , b , c , n ) = ∑ i = 0 n ⌊ a i + b c ⌋ 2 h(a,b,c,n)=\sum_{i=0}^n\left\lfloor\frac{ai+b}c\right\rfloor^2 h(a,b,c,n)=i=0ncai+b2

由于求值的过程跟求 g c d gcd gcd的欧几里得算法类似因此叫做类欧几里得。

求f

  1. a ≥ c a\ge c ac o r or or b ≥ c b\ge c bc
    ⌊ a i + b c ⌋ \left\lfloor\frac{ai+b}c\right\rfloor cai+b拆开变成 ⌊ ( a % c ) i + b % c c ⌋ + ⌊ a c ⌋ i + ⌊ b c ⌋ \left\lfloor\frac{(a\% c)i+b\%c}c\right\rfloor+\left\lfloor\frac ac\right\rfloor i+\left\lfloor\frac bc\right\rfloor c(a%c)i+b%c+cai+cb
    f ( a , b , c , n ) ⇔ f ( a % c , b % c , c , n ) + n ( n + 1 ) 2 ⌊ a c ⌋ + ( n + 1 ) ⌊ b c ⌋ f(a,b,c,n)\Leftrightarrow f(a\%c,b\%c,c,n)+\frac{n(n+1)}2\left\lfloor\frac ac\right\rfloor+(n+1)\left\lfloor\frac bc\right\rfloor f(a,b,c,n)f(a%c,b%c,c,n)+2n(n+1)ca+(n+1)cb
  2. a < c a<c a<c a n d and and b < c b<c b<c
    a = 0 ⇒ f ( a , b , c , n ) = 0 a=0\Rightarrow f(a,b,c,n)=0 a=0f(a,b,c,n)=0
    a ̸ = 0 : a\not=0: a̸=0:
    f ( a , b , c , n ) = ∑ i = 0 n ⌊ a i + b c ⌋ = ∑ i = 0 n ∑ j = 0 ⌊ a i + b c ⌋ − 1 1 = ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n [ j < ⌊ a i + b c ⌋ ] = ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n [ j < ⌈ a i + b − c + 1 c ⌉ ] = ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n [ c j < a i + b − c + 1 ] = ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n [ a i > c j − b + c − 1 ] = ∑ j = 0 ⌊ a n + b c ⌋ − 1 ∑ i = 0 n [ i > ⌊ c j − b + c − 1 a ⌋ ] = ∑ j = 0 ⌊ a n + b c ⌋ − 1 [ n − ⌊ c j − b + c − 1 a ⌋ ] = n ⌊ a i + b c ⌋ − f ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) ; \begin{aligned} f(a,b,c,n)=&\sum_{i=0}^n\left\lfloor\frac{ai+b}c\right\rfloor\\ =&\sum_{i=0}^n\sum_{j=0}^{\left\lfloor\frac{ai+b}c\right\rfloor-1}1\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}\sum_{i=0}^n[j<\left\lfloor\frac{ai+b}c\right\rfloor]\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}\sum_{i=0}^n[j<\left\lceil\frac{ai+b-c+1}c\right\rceil]\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}\sum_{i=0}^n[cj<ai+b-c+1]\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}\sum_{i=0}^n[ai>cj-b+c-1]\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}\sum_{i=0}^n[i>\left\lfloor\frac{cj-b+c-1}a\right\rfloor]\\ =&\sum_{j=0}^{\left\lfloor\frac{an+b}c\right\rfloor-1}[n-\left\lfloor\frac{cj-b+c-1}a\right\rfloor]\\ =&n\left\lfloor\frac{ai+b}c\right\rfloor-f(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1); \end{aligned} f(a,b,c,n)=========i=0ncai+bi=0nj=0cai+b11j=0can+b1i=0n[j<cai+b]j=0can+b1i=0n[j<cai+bc+1]j=0can+b1i=0n[cj<ai+bc+1]j=0can+b1i=0n[ai>cjb+c1]j=0can+b1i=0n[i>acjb+c1]j=0can+b1[nacjb+c1]ncai+bf(c,b+c1,a,can+b1);
    然后这个时候 c c c是比 a a a大的,所以在计算 f ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) f(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1) f(c,b+c1,a,can+b1)的时候会变成第一种情况,相当于原本的 a , c a,c a,c对应的参数变成了 ( c , a % c ) (c,a\%c) (c,a%c),因此该算法时间复杂度跟求 g c d gcd gcd相同,为 l o g log log级别的。
    g , h g,h g,h推导类似直接上式子了。

求g

  1. a ≥ c a\ge c ac o r or or b ≥ c : g ( a , b , c , n ) = g ( a % c , b % c , c , n ) + n ( n + 1 ) ( 2 n + 1 ) 6 ⌊ a c ⌋ + n ( n + 1 ) 2 ⌊ b c ⌋ b\ge c:g(a,b,c,n)=g(a\%c,b\%c,c,n)+\frac{n(n+1)(2n+1)}6\left\lfloor\frac ac\right\rfloor+\frac{n(n+1)}2\left\lfloor\frac bc\right\rfloor bc:g(a,b,c,n)=g(a%c,b%c,c,n)+6n(n+1)(2n+1)ca+2n(n+1)cb
  2. a < c a<c a<c a n d and and b < c : g ( a , b , c , n ) = ⌊ a n + b c ⌋ n ( n + 1 ) − f ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) − h ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) 2 b<c:g(a,b,c,n)=\frac{\left\lfloor\frac{an+b}c\right\rfloor n(n+1)-f(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1)-h(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1)}2 b<c:g(a,b,c,n)=2can+bn(n+1)f(c,b+c1,a,can+b1)h(c,b+c1,a,can+b1)

求h

  1. a ≥ c a\ge c ac o r or or b ≥ c : h ( a , b , c , n ) = h ( a % c , b % c , c , n ) + n ( n + 1 ) ( 2 n + 1 ) 6 ⌊ a c ⌋ 2 + ( n + 1 ) ⌊ b c ⌋ 2 + 2 ⌊ b c ⌋ f ( a % c , b % c , c , n ) + 2 ⌊ a c ⌋ g ( a % c , b % c , c , n ) + ⌊ a c ⌋ ⌊ b c ⌋ n ( n + 1 ) b\ge c:h(a,b,c,n)=h(a\%c,b\%c,c,n)+\frac{n(n+1)(2n+1)}6\left\lfloor\frac ac\right\rfloor^2+(n+1)\left\lfloor\frac bc\right\rfloor^2+2\left\lfloor\frac bc\right\rfloor f(a\%c,b\%c,c,n)+2\left\lfloor\frac ac\right\rfloor g(a\%c,b\%c,c,n)+\left\lfloor\frac ac\right\rfloor\left\lfloor\frac bc\right\rfloor n(n+1) bc:h(a,b,c,n)=h(a%c,b%c,c,n)+6n(n+1)(2n+1)ca2+(n+1)cb2+2cbf(a%c,b%c,c,n)+2cag(a%c,b%c,c,n)+cacbn(n+1)
  2. a < c a<c a<c a n d and and b < c : h ( a , b , c , n ) = ⌊ a n + b c ⌋ ( ⌊ a n + b c ⌋ + 1 ) n − 2 g ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) − 2 f ( c , − b + c − 1 , a , ⌊ a n + b c ⌋ − 1 ) − f ( a , b , c , n ) b<c:h(a,b,c,n)=\left\lfloor\frac{an+b}c\right\rfloor(\left\lfloor\frac{an+b}c\right\rfloor+1) n-2g(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1)-2f(c,-b+c-1,a,\left\lfloor\frac{an+b}c\right\rfloor-1)-f(a,b,c,n) b<c:h(a,b,c,n)=can+b(can+b+1)n2g(c,b+c1,a,can+b1)2f(c,b+c1,a,can+b1)f(a,b,c,n)

你可能感兴趣的:(#,数学,#,类欧几里得)