Euclidean and Extended Euclidean Algorithm (including derivation process and code)

foreword

I used the extended Euclidean algorithm when I was studying the eighth lecture of the Acwing c++ Blue Bridge Cup Tutorial Course - AcWing 1299. Wuzhishan , here are the knowledge points.

The current article has been included in the blog file directory index: blog directory index (continuously updated)

1. Euclidean Algorithm

Introduction :

Euclidean algorithm: Euclidean algorithm , also known as the rolling and dividing method , is used to calculate the greatest common divisor of two integers a and b.

  • gcd(a, b) = d, the d is the greatest common divisor.

code :

class Solution {
    
    

    //欧几里得算法(辗转相除法)
    public static int gcd(int a, int b) {
    
    
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static void main(String[] args) {
    
    
        System.out.println(gcd(12, 5));
    }
}

2. Extended Euclidean Algorithm

2.1. Understanding Pei Shu Theorem

Peishu's theorem, also known as Beizu's theorem (extended Euclidean): for any integer a, b and their greatest common divisor d, the linear indeterminate equation about the unknowns x and y (called Peishu's equation) , is a theorem about the greatest common divisor.

If a, b are integers, and gcd(a, b) = d, then there must be integers x, y such that ax+by=d holds true.

2.2. Deduce ax+by=gcd(a, b) to get x and y

2.2.1. Derivation process

For the equation: ax+by=gcd(a, b)

①When b = 0, ax + by = a, then x = 1, y = 0. [In fact, it is when the greatest common divisor is found to be critical]

②When b ≠ 0, namely gcd(a, b) = gcd(b, a % b), according to Pei Shu's theorem gcd(a, b) = d => ax+by=d

gcd(b, a % b) = bx' + (a % b)y', and a % b = a - (int)(a / b) * b, where this a/b is an integer, that is, a % b = a - a/b*b

gcd(b, a % b) = bx' + (a - a/b*b)y', and then disassemble the formula as ay' + b(x' - (a/b) * y)

At this point, the formula is obtained: gcd(b, a % b) = ay' + b(x' - (a/b) * y')

又gcd(a, b) = ax + by,又gcd(a, b) = gcd(b, a % b)

where ax + by = ay' + b(x' - (a/b) * y')

  • x = y’
  • y = x’ - (a/b) * y’

This y', x' refers to x, y at the current recursion level.

2.2.2, code implementation

class Solution {
    
    

    /**
     * x.a + b * y = d
     * @param a
     * @param b
     * @param arr 存储x,y
     */
    public static int exGcd(int a, int b, int[] arr) {
    
    
        if (b == 0) {
    
    
            //此时已经求得最大公约数即为a,此时默认x为1,y为0
            arr[0] = 1;
            arr[1] = 0;
            return a;
        }
        //递归求得最大公约数
        int d = exGcd(b, a % b, arr);
        //通过公式去化解转为 x = y',y = x‘ - a/b*y'
        int temp = arr[0];
        arr[0] = arr[1];
        arr[1] = temp - a / b * arr[1];
        return d;
    }

    public static void main(String[] args) {
    
    
        //扩展欧几里得计算得到x,y
        int[] arr = new int[2];
        System.out.printf("最大公约数为:%d\n", exGcd(12, 5, arr));
        System.out.printf("x = %d, y = %d", arr[0], arr[1]);
    }
}

image-20230124164526480


2.3. Deduce all solutions of ax+by=gcd(a, b) and the minimum value of a or b (conclusion + verification)

Using the extended Euclidean algorithm, we can derive ax+by=gcd(a, b) to obtain a set of x and y solutions.

in conclusion

Through such a set of x and y solutions we can get all x and y solutions !

Here we use gcd(a,b) to represent d, ax+by = d.

Definition : a' = ad \frac{a}{d}da,b‘ = b d \frac{b}{d} db

x and y formula definition conclusion : all x, y solutions are

x = x0 + kb'
y = y0 - ka'

The conclusion of the minimum positive integer value of x or y (here is the minimum positive integer value of y): y = y % a', at this time the minimum value of y can be obtained.

  • We can directly substitute y of the first set of solutions or use the obtained y0 to get the smallest positive integer solution!

Related algorithm topic: AcWing 1299. Wuzhishan

proof derivation

①X and y formula definition conclusion derivation

Let's try to bring the final derivation formula of x and y into the ax+by = d formula:

ax+by = a(x0 + kb') + b(y0 - to') = ax0 + akb' + by0 - bka' = ax0 + by0 + akb' - bka'

At this point we will a' = ad \frac{a}{d}da,b‘ = b d \frac{b}{d} dbSubstitute into akb' - bka', akb' = ak.b / d, bka' = bk.a / d, that is to say akb' = bka'

So ax0 + by0 + akb' - bka' = ax0 + by0 = d, proved!

Let's take an example to prove whether the corresponding a' and b' and x and y general formulas are correct:

给出a = 12,b = 5,d = gcd(a, b) = 1
根据扩展欧几里得可以得到式子:ax + by = d
	通过算法来得到一组x、y解,x = -2,y = 5      // 式子为:-2*12 + 5*5 = 1
接着我们根据①中的公式定理,a' = a / d,b' = b / d  //  a' = 12/1 = 12, b' = 5 / 1 = 5
对应x与y的所有解公式为:x = x0 + kb',y = y0 - ka'

此时我们可以去尝试得到x0的解(假设k=1)-2 = x0 + 1*5,得到x0 = -7
     尝试得到y0的解(假设k=1)5 = y0 - 1*12,得到y0 = 17
    
ok,此时x0 = -7,y0 = 17,我们来自己通过公式去尝试构造一组解,设置k = 2,
   	 x = -7 + 2 * 5 = 3
 	 y = 17 - 2 * 12 = -7
尝试代入ax+by=d中,即为12 * 3 + 5 * (-7) = 1,此时能够验证成功!

②Example of the minimum positive integer value of y: y = y % a'

Still using the example in ①, let's find the minimum value of y: a = 12, b = 5, a' = 12, b' = 5, x0 = -7, y0 = 17

//使用y0代入
17 % 12 = 5
    
//使用y代入(第一组解得到的值y = 5)
5 % 12 = 5
    
//使用y为负数情况代入(自己设置k=2时得到的y解)
-7 % 12 = -5
//对于该特殊情况我们需要使用(y mod a' + a') % a'
(-7 % 12 + 12) % 12 = (-5 + 12) % 12 = 17 % 12 = 5

Conclusion: We can directly substitute the y of the first set of solutions or use the obtained y0 to get the smallest positive integer solution!

reference article

[1]. Extended Euclidean algorithm idea and template code

[2]. Detailed Explanation of Extended Euclidean Algorithm

Guess you like

Origin blog.csdn.net/cl939974883/article/details/128757749