Skip to content

Commit b3c8bdd

Browse files
committed
Merge branch 'master' into mkdocs
2 parents 9020a54 + 09d91f2 commit b3c8bdd

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/algebra/binary-exp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,4 @@ $$a \cdot b = \begin{cases}
233233
* [SPOJ - Locker](http://www.spoj.com/problems/LOCKER/)
234234
* [LA - 3722 Jewel-eating Monsters](https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1723)
235235
* [SPOJ - Just add it](http://www.spoj.com/problems/ZSUM/)
236+
* [Codechef - Chef and Riffles](https://www.codechef.com/JAN221B/problems/RIFFLES)

src/algebra/euclid-algorithm.md

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it
2020

2121
## Algorithm
2222

23-
The algorithm is extremely simple:
23+
Originally, the Euclidean algorithm was formulated as follows: subtract the smaller number from the larger one until one of the numbers is zero. Indeed, if $g$ divides $a$ and $b$, it also divides $a-b$. On the other hand, if $g$ divides $a-b$ and $b$, then it also divides $a = b + (a-b)$, which means that the sets of the common divisors of $\{a, b\}$ and $\{b,a-b\}$ coincide.
24+
25+
Note that $a$ remains the larger number until $b$ is subtracted from it at least $\left\lfloor\frac{a}{b}\right\rfloor$ times. Therefore, to speed things up, $a-b$ is substituted with $a-\left\lfloor\frac{a}{b}\right\rfloor b = a \bmod b$. Then the algorithm is formulated in an extremely simple way:
2426

2527
$$\gcd(a, b) = \begin{cases}a,&\text{if }b = 0 \\ \gcd(b, a \bmod b),&\text{otherwise.}\end{cases}$$
2628

@@ -55,29 +57,7 @@ int gcd (int a, int b) {
5557
}
5658
```
5759
58-
## Correctness Proof
59-
60-
First, notice that in each iteration of the Euclidean algorithm the second argument strictly decreases, therefore (since the arguments are always non-negative) the algorithm will always terminate.
61-
62-
For the proof of correctness, we need to show that $\gcd(a, b) = \gcd(b, a \bmod b)$ for all $a \geq 0$, $b > 0$.
63-
64-
We will show that the value on the left side of the equation divides the value on the right side and vice versa. Obviously, this would mean that the left and right sides are equal, which will prove Euclid's algorithm.
65-
66-
Let $d = \gcd(a, b)$. Then by definition $d\mid a$ and $d\mid b$.
67-
68-
Now let's represent the remainder of the division of $a$ by $b$ as follows:
69-
70-
$$a \bmod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor$$
71-
72-
From this it follows that $d \mid (a \bmod b)$, which means we have the system of divisibilities:
73-
74-
$$\begin{cases}d \mid b,\\ d \mid (a \mod b)\end{cases}$$
75-
76-
Now we use the fact that for any three numbers $p$, $q$, $r$, if $p\mid q$ and $p\mid r$ then $p\mid \gcd(q, r)$. In our case, we get:
77-
78-
$$d = \gcd(a, b) \mid \gcd(b, a \mod b)$$
79-
80-
Thus we have shown that the left side of the original equation divides the right. The second half of the proof is similar.
60+
Note that since C++17, `gcd` is implemented as a [standard function](https://en.cppreference.com/w/cpp/numeric/gcd) in C++.
8161
8262
## Time Complexity
8363
@@ -89,6 +69,8 @@ Moreover, it is possible to show that the upper bound of this theorem is optimal
8969
9070
Given that Fibonacci numbers grow exponentially, we get that the Euclidean algorithm works in $O(\log \min(a, b))$.
9171
72+
Another way to estimate the complexity is to notice that $a \bmod b$ for the case $a \geq b$ is at least $2$ times smaller than $a$, so the larger number is reduced at least in half on each iteration of the algorithm.
73+
9274
## Least common multiple
9375
9476
Calculating the least common multiple (commonly denoted **LCM**) can be reduced to calculating the GCD with the following simple formula:
@@ -117,7 +99,7 @@ It's based on a few properties:
11799

118100
- If both numbers are even, then we can factor out a two of both and compute the GCD of the remaining numbers: $\gcd(2a, 2b) = 2 \gcd(a, b)$.
119101
- If one of the numbers is even and the other one is odd, then we can remove the factor 2 from the even one: $\gcd(2a, b) = \gcd(a, b)$ if $b$ is odd.
120-
- If both numbers are odd, then subtracting one number of the other one will not change the GCD: $\gcd(a, b) = \gcd(b, a-b)$ (this can be proven in the same way as the correctness proof of the normal algorithm)
102+
- If both numbers are odd, then subtracting one number of the other one will not change the GCD: $\gcd(a, b) = \gcd(b, a-b)$
121103

122104
Using only these properties, and some fast bitwise functions from GCC, we can implement a fast version:
123105

src/algebra/linear-diophantine-equation.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,33 @@ In this article, we consider several classical problems on these equations:
2222

2323
A degenerate case that need to be taken care of is when $a = b = 0$. It is easy to see that we either have no solutions or infinitely many solutions, depending on whether $c = 0$ or not. In the rest of this article, we will ignore this case.
2424

25-
## Finding a solution
25+
## Analytic solution
26+
27+
When $a \neq 0$ and $b \neq 0$, the equation $ax+by=c$ can be equivalently treated as either of the following:
28+
29+
\begin{gather}
30+
ax \equiv c \pmod b,\newline
31+
by \equiv c \pmod a.
32+
\end{gather}
33+
34+
Without loss of generality, assume that $b \neq 0$ and consider the first equation. When $a$ and $b$ are co-prime, the solution to it is given as
35+
36+
$$x \equiv ca^{-1} \pmod b,$$
37+
38+
where $a^{-1}$ is the [modular inverse](module-inverse.md) of $a$ modulo $b$.
39+
40+
When $a$ and $b$ are not co-prime, values of $ax$ modulo $b$ for all integer $x$ are divisible by $g=\gcd(a, b)$, so the solution only exists when $c$ is divisible by $g$. In this case, one of solutions can be found by reducing the equation by $g$:
41+
42+
$$(a/g) x \equiv (c/g) \pmod{b/g}.$$
43+
44+
By the definition of $g$, the numbers $a/g$ and $b/g$ are co-prime, so the solution is given explicitly as
45+
46+
$$\begin{cases}
47+
x \equiv (c/g)(a/g)^{-1}\pmod{b/g},\\
48+
y = \frac{c-ax}{b}.
49+
\end{cases}$$
50+
51+
## Algorithmic solution
2652

2753
To find one solution of the Diophantine equation with 2 unknowns, you can use the [Extended Euclidean algorithm](extended-euclid-algorithm.md). First, assume that $a$ and $b$ are non-negative. When we apply Extended Euclidean algorithm for $a$ and $b$, we can find their greatest common divisor $g$ and 2 numbers $x_g$ and $y_g$ such that:
2854

0 commit comments

Comments
 (0)