Bezout's lemma reveals a profound connection between the greatest common divisor and integer linear combinations. It is one of the most fundamental and important results in number theory. Based on it, this article further discusses methods for solving linear Diophantine equations.
Bezout's Lemma
Bezout's lemma, also translated as Bezout's theorem or called Bezout's identity, gives the necessary and sufficient condition for an integer to be represented as an integer-coefficient linear combination of two integers.
Bezout's Lemma
Let be integers that are not both zero. Then, for any integers , holds; moreover, there exist integers such that .
Proof
Let . Since , there exist integers such that . Therefore,
This shows that .
Conversely, we need to show that there exist for which the equality holds. If one of is , assume without loss of generality that . Their greatest common divisor is then , and clearly makes the equality hold. Now consider the case in which are both nonzero. Since , assume without loss of generality that both are positive.
Consider the process of the Euclidean algorithm:
Since the greatest common divisor is , the final remainder must satisfy . Thus, the penultimate equation can be written as
Solving the antepenultimate equation for the preceding remainder gives
Substituting this into the equation above eliminates :
Continuing in the same way, we can eliminate all of and eventually obtain
This proves that there exist such that . The preceding analysis therefore proves the original proposition.
The proof of existence here is constructive; it also gives a way to calculate the coefficients. This method is the extended Euclidean algorithm.
Considering the special case of Bezout's lemma where gives the following corollary:
Corollary
Integers are coprime if and only if there exist integers such that .
The Case of Multiple Integers
Bezout's lemma can be generalized to multiple integers.
Theorem
Let be integers that are not all zero. Then, for any integers , holds; moreover, there exist integers such that .
Proof
Apply induction on using .
Example Problem
Codeforces 510 D. Fox And Jumping
There are cards, with and respectively. On an infinitely long strip of paper, you may spend to buy card , after which you can jump units to the left or right any number of times. What is the minimum amount of money needed to reach every position on the strip? If this is impossible, output .
Solution
Analyzing the problem, to reach every cell, the absolute value obtained by adding or subtracting the selected numbers some number of times must be . In other words, there must exist integers such that . By Bezout's lemma for multiple integers, this is equivalent to selecting some numbers from the array whose greatest common divisor is , while minimizing the sum of their costs.
Solution 1: Regard the minimum cost sum as a shortest-path problem and solve it with Dijkstra's algorithm. Store the current greatest common divisor at each graph vertex. The starting vertex is , and the target vertex is . At each step, start from the current vertex and follow an edge of length to the vertex . This algorithm has time complexity .
Solution 2: Selecting some numbers from so that their greatest common divisor is and their cost sum is minimized suggests a 0-1 knapsack problem.
Let denote the minimum cost when considering the first numbers and having greatest common divisor . The transition is
After the DP, the final total cost is .
As with the ordinary 0-1 knapsack problem, a rolling array can remove the first dimension. Here, the greatest common divisors that can be formed by 300 numbers are sparse, so a hash table can be used to store them.
In fact, the graph constructed in Solution 1 is precisely the state-transition graph of the dynamic programming in Solution 2. Solution 2 is equivalent to finding shortest paths in a directed acyclic graph with dynamic programming. Thus, the two solutions are equivalent. However, Solution 2 does not need to store the entire graph, and its DP time complexity is , lower than that of Dijkstra's algorithm, so Solution 2 is better in both time and space.
Linear Diophantine Equations
A linear Diophantine equation is an indeterminate equation of the form
where are all integers. The goal of this section is to find all of its integer solutions.
The Two-Variable Case
First consider the two-variable linear Diophantine equation
Bezout's lemma states that this equation has a solution if and only if
Now assume that this condition holds. The extended Euclidean algorithm can find an integer solution to . This gives a particular solution of the original equation:
To obtain all solutions, subtract the identity from the original equation, yielding
This is a homogeneous linear Diophantine equation in , with general solution
Therefore, the general solution of the original equation is
These are a series of equally spaced integer points on the line .
The Multiple-Variable Case
Once the two-variable case is solved, the multivariable case is easy as well. For an -variable linear Diophantine equation
Bezout's lemma shows that the equation has a solution if and only if
As in the two-variable case, the general solution of a multivariable linear Diophantine equation can also be written in the form
where is a particular solution and are the solutions of the corresponding homogeneous equation.
The explicit form of the general solution can be obtained by converting the -variable equation into an -variable equation. Let . By Bezout's lemma, all values of are exactly the multiples of . Thus, first solve the -variable linear Diophantine equation
Suppose its general solution is
Let be a particular solution of . Then, by the discussion in the previous section, the general solution in of is
Substituting the expression for gives the general solution of the original equation:
Frobenius Coin Problem
Bezout's lemma gives the necessary and sufficient condition for an integer to be represented as an integer linear combination of several integers. Closely related to it is the Frobenius coin problem:
- If there are coins with integer denominations and , what is the largest integer that cannot be formed from these coins?
This also asks when an integer can be represented in the form . In Bezout's lemma, may be arbitrary integers, whereas in the Frobenius coin problem, may only be natural numbers.
The case of only one coin is trivial, since necessarily and every natural number can be represented by it. The case is too complicated, so this section discusses only .
Sylvester's Theorem
In 1882, Sylvester completely solved the Frobenius coin problem for :
Theorem (Sylvester)
For coprime positive integers , the largest integer that cannot be written as is . Moreover, for every , exactly one of the integers and can be written in this form.
For convenience, call an integer that can be written in the form representable.
Proof 1
Since are coprime, for every integer , the equation has a solution, whose general form is
Let be the quotient obtained when dividing by with remainder. Then the remainder lies between and . Consider the solution obtained this way. Since is the smallest nonnegative value it can take, is representable if and only if .
Step 1: Prove that every integer greater than is representable.
When ,
Thus, , that is, . Therefore, is a solution in natural numbers, and can be written in the required form.
Step 2: Prove that is not representable. Consequently, is the largest non-representable integer, and and cannot both be representable.
We use contradiction. Suppose that is representable, so there exist such that . Substituting the expression for gives
Therefore, and . Since are both positive,
This is a contradiction, so is not representable. Together with Step 1, it is the largest non-representable integer.
If both and were representable, adding the coefficients in the representations of and would produce a representation of , contradicting the fact that is not representable. Hence at most one of and can be representable.
Step 3: Prove that if is not representable, then is representable.
Let be the integer solution of introduced above. The preceding discussion shows that is not representable if and only if . Therefore,
Both and are nonnegative integers, so is representable.
Proof 2
Here we prove only that is the largest non-representable natural number; the rest is proved similarly to Proof 1.
Consider, modulo , the smallest representable natural number in each residue class. Since different natural numbers in the same residue class can be transformed into one another by adding or subtracting some multiple of , when considering the smallest representable number it is enough to consider adding or subtracting . Since and are coprime, the smallest representable natural numbers in the residue classes are exactly the multiples of :
Therefore, the largest non-representable number is
Geometric Interpretation
View the equation as a line. Then is representable if and only if this line passes through an integer point in the first quadrant, including the coordinate axes. When , this line can pass through at most one integer point in the first quadrant. Therefore, for , an integer is representable if and only if the line for passes through exactly one integer point in the first quadrant.
Consequently, the number of representable natural numbers satisfying is exactly the number of integer points below the line in the first quadrant, including boundary points. This number equals
This is the classic problem of counting integer points under a line, which can be solved in time using the floor-sum Euclidean algorithm.