ACM Number Theory Pei Shu Theorem (Bezu Theorem)

1. Content definition

        "Pei Shu Theorem", also known as Bézout's lemma. is a theorem about the greatest common divisor. Its content is defined as: For any integers a and b that are not all zero, record the greatest common divisor of the two as g, that is, gcd(a,b) = g , then for any integer x and y must satisfy ax+by is g multiples of . In particular, there must exist a solution for the integers x and y such that ax+by=gcd(a,b) holds . One of its important inferences is: the necessary and sufficient condition for the mutual prime of a and b is the existence of integers x and y so that ax+by=1 ; or for the equation  ax+by=1  only when the integers a and b are relatively prime, the equation has Integer solution x,y.

        "Pei Shu Theorem" can also be extended to the case of multiple integers. For any n integers that are not all zero  a_1,a_2,a_3,...,a_n, record the greatest common divisor of these n numbers as  g = gcd(a_1,a_2,...,a_n), then for any n integers,  x_1,x_2,...,x_n they are all  \sum_{1}^{n} a_i*x_i multiples of g. In particular, there must exist a solution to a sequence of integers  suchx_1,x_2,...,x_n  that  . An important corollary of it is  :  the necessary and sufficient condition for the greatest common divisor of positive integers ​to​ to be 1  is that there are n integers  to   satisfy x_1*a_1+x_2*a_2+...+x_n*a_n = g a_1a_nx_1x_nx_1*a_1+x_2*a_2+...+x_n*a_n = 1

2. Proof and application

1. Proof

        The proof of Pei Shu's theorem will not be repeated in this article. This theorem is a very simple but very important basic theorem. Here are two more official proofs, please refer to the following:

2. Application

        As a very important basic theorem, Pei Shu theorem can appear as a key problem-solving idea in some algorithm problems; on the other hand, this theorem is also the derivation basis of some algorithms and proofs, such as the extended Euclidean algorithm, Linear congruence equations, etc. You can refer to my previous article for more clarity:

3. Examples

1. "Check the array" LeetCode 1250

        Given an array nums of positive integers, you need to choose some subsets from it, then multiply each number in the subset by an arbitrary integer, and find their sum. If the result of this sum is 1, then the original array is a "good array", return True; otherwise, return False.

        The requirement of the original question can be transformed into finding whether it exists in the array  x_1*nums_1 + x_2*nums_2 +... + x_k*nums_k = 1 . According to Pei Shu's theorem, the problem is to find out whether there is any set of coprime numbers in the array.

         If we think about it positively, the problem is more complicated. We need to consider whether there are two coprime, three coprime..., and the time complexity is high. But if you think about it from the opposite side , the problem is much simpler: an array is either a "good array" or a "not good array"; if the entire array is a "not good array", it means that there is no set of coprime (any two are not mutually prime) , then we can directly find  nums_1,nums_2,....,nums_n the greatest common divisor of the entire array. If the greatest common divisor of all numbers is equal to 1, the original array is a "good array", otherwise it is not.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }

    bool isGoodArray(vector<int>& nums) {
        int len = nums.size();
        int x = nums[0];
        for(int i = 1;i<len;i++){
            if(x==1)break;
            x = gcd(x,nums[i]);
        }
        return x == 1;
    }
};

Guess you like

Origin blog.csdn.net/qq_40772692/article/details/129099553