You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/binary-exp.md
+37-33Lines changed: 37 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<!--?title Binary Exponentiation-->
1
+
---
2
+
title: Binary Exponentiation
3
+
hide:
4
+
- navigation
5
+
---
2
6
# Binary Exponentiation
3
7
4
8
Binary exponentiation (also known as exponentiation by squaring) is a trick which allows to calculate $a^n$ using only $O(\log n)$ multiplications (instead of $O(n)$ multiplications required by the naive approach).
@@ -30,9 +34,9 @@ So we only need to know a fast way to compute those.
30
34
Luckily this is very easy, since an element in the sequence is just the square of the previous element.
31
35
32
36
$$\begin{align}
33
-
3^1 &= 3 \\\\
34
-
3^2 &= \left(3^1\right)^2 = 3^2 = 9 \\\\
35
-
3^4 &= \left(3^2\right)^2 = 9^2 = 81 \\\\
37
+
3^1 &= 3 \\
38
+
3^2 &= \left(3^1\right)^2 = 3^2 = 9 \\
39
+
3^4 &= \left(3^2\right)^2 = 9^2 = 81 \\
36
40
3^8 &= \left(3^4\right)^2 = 81^2 = 6561
37
41
\end{align}$$
38
42
@@ -44,9 +48,9 @@ The final complexity of this algorithm is $O(\log n)$: we have to compute $\log
44
48
The following recursive approach expresses the same idea:
45
49
46
50
$$a^n = \begin{cases}
47
-
1 &\text{if } n == 0 \\\\
48
-
\left(a^{\frac{n}{2}}\right)^2 &\text{if } n > 0 \text{ and } n \text{ even}\\\\
49
-
\left(a^{\frac{n - 1}{2}}\right)^2 \cdot a &\text{if } n > 0 \text{ and } n \text{ odd}\\\\
51
+
1 &\text{if } n == 0 \\
52
+
\left(a^{\frac{n}{2}}\right)^2 &\text{if } n > 0 \text{ and } n \text{ even}\\
53
+
\left(a^{\frac{n - 1}{2}}\right)^2 \cdot a &\text{if } n > 0 \text{ and } n \text{ odd}\\
50
54
\end{cases}$$
51
55
52
56
## Implementation
@@ -107,7 +111,7 @@ long long binpow(long long a, long long b, long long m) {
107
111
}
108
112
```
109
113
110
-
**Note:** If $m$ is a prime number we can speed up a bit this algorithm by calculating $x ^ {n \mod (m-1)}$ instead of $x ^ n$.
114
+
**Note:** If $m$ is a prime number we can speed up a bit this algorithm by calculating $x^{n \bmod (m-1)}$ instead of $x ^ n$.
111
115
This follows directly from [Fermat's little theorem](module-inverse.md#toc-tgt-2).
112
116
113
117
### Effective computation of Fibonacci numbers
@@ -122,7 +126,7 @@ the transition from $F_i$ and $F_{i+1}$ to $F_{i+1}$ and $F_{i+2}$.
122
126
For example, applying this transformation to the pair $F_0$ and $F_1$ would change it into $F_1$ and $F_2$.
123
127
Therefore, we can raise this transformation matrix to the $n$-th power to find $F_n$ in time complexity $O(\log n)$.
124
128
125
-
### Applying a permutation $k$ times
129
+
### Applying a permutation $k$ times { data-toc-label='Applying a permutation <script type="math/tex">k</script> times' }
126
130
127
131
**Problem:** You are given a sequence of length $n$. Apply to it a given permutation $k$ times.
128
132
@@ -143,20 +147,20 @@ Therefore, we can raise this transformation matrix to the $n$-th power to find $
143
147
As you can see, each of the transformations can be represented as a linear operation on the coordinates. Thus, a transformation can be written as a $4 \times 4$ matrix of the form:
144
148
145
149
$$\begin{pmatrix}
146
-
a_{11} & a_ {12} & a_ {13} & a_ {14} \\\
147
-
a_{21} & a_ {22} & a_ {23} & a_ {24} \\\
148
-
a_{31} & a_ {32} & a_ {33} & a_ {34} \\\
149
-
a_{41} & a_ {42} & a_ {43} & a_ {44} \\\
150
+
a_{11} & a_ {12} & a_ {13} & a_ {14} \\
151
+
a_{21} & a_ {22} & a_ {23} & a_ {24} \\
152
+
a_{31} & a_ {32} & a_ {33} & a_ {34} \\
153
+
a_{41} & a_ {42} & a_ {43} & a_ {44}
150
154
\end{pmatrix}$$
151
155
152
156
that, when multiplied by a vector with the old coordinates and an unit gives a new vector with the new coordinates and an unit:
153
157
154
158
$$\begin{pmatrix} x & y & z & 1 \end{pmatrix} \cdot
@@ -167,34 +171,34 @@ Here are some examples of how transformations are represented in matrix form:
167
171
* Shift operation: shift $x$ coordinate by $5$, $y$ coordinate by $7$ and $z$ coordinate by $9$.
168
172
169
173
$$\begin{pmatrix}
170
-
1 & 0 & 0 & 0 \\\
171
-
0 & 1 & 0 & 0 \\\
172
-
0 & 0 & 1 & 0 \\\
173
-
5 & 7 & 9 & 1 \\\
174
+
1 & 0 & 0 & 0 \\
175
+
0 & 1 & 0 & 0 \\
176
+
0 & 0 & 1 & 0 \\
177
+
5 & 7 & 9 & 1
174
178
\end{pmatrix}$$
175
179
176
180
* Scaling operation: scale the $x$ coordinate by $10$ and the other two by $5$.
177
181
178
182
$$\begin{pmatrix}
179
-
10 & 0 & 0 & 0 \\\
180
-
0 & 5 & 0 & 0 \\\
181
-
0 & 0 & 5 & 0 \\\
182
-
0 & 0 & 0 & 1 \\\
183
+
10 & 0 & 0 & 0 \\
184
+
0 & 5 & 0 & 0 \\
185
+
0 & 0 & 5 & 0 \\
186
+
0 & 0 & 0 & 1
183
187
\end{pmatrix}$$
184
188
185
189
* Rotation operation: rotate $\theta$ degrees around the $x$ axis following the right-hand rule (counter-clockwise direction).
186
190
187
191
$$\begin{pmatrix}
188
-
1 & 0 & 0 & 0 \\\
189
-
0 & \cos \theta & -\sin \theta & 0 \\\
190
-
0 & \sin \theta & \cos \theta & 0 \\\
191
-
0 & 0 & 0 & 1 \\\
192
+
1 & 0 & 0 & 0 \\
193
+
0 & \cos \theta & -\sin \theta & 0 \\
194
+
0 & \sin \theta & \cos \theta & 0 \\
195
+
0 & 0 & 0 & 1
192
196
\end{pmatrix}$$
193
197
194
198
Now, once every transformation is described as a matrix, the sequence of transformations can be described as a product of these matrices, and a "loop" of $k$ repetitions can be described as the matrix raised to the power of $k$ (which can be calculated using binary exponentiation in $O(\log{k})$). This way, the matrix which represents all transformations can be calculated first in $O(m \log{k})$, and then it can be applied to each of the $n$ points in $O(n)$ for a total complexity of $O(n + m \log{k})$.
195
199
196
200
197
-
### Number of paths of length $k$ in a graph
201
+
### Number of paths of length $k$ in a graph { data-toc-label='Number of paths of length <script type="math/tex">k</script> in a graph' }
198
202
199
203
**Problem:** Given a directed unweighted graph of $n$ vertices, find the number of paths of length $k$ from any vertex $u$ to any other vertex $v$.
200
204
@@ -205,15 +209,15 @@ Instead of the usual operation of multiplying two matrices, a modified one shoul
205
209
instead of multiplication, both values are added, and instead of a summation, a minimum is taken.
206
210
That is: $result_{ij} = \min\limits_{1\ \leq\ k\ \leq\ n}(a_{ik} + b_{kj})$.
207
211
208
-
### Variation of binary exponentiation: multiplying two numbers modulo $m$
212
+
### Variation of binary exponentiation: multiplying two numbers modulo $m$ { data-toc-label='Variation of binary exponentiation: multiplying two numbers modulo <script type="math/tex">m</script>' }
209
213
210
214
**Problem:** Multiply two numbers $a$ and $b$ modulo $m$. $a$ and $b$ fit in the built-in data types, but their product is too big to fit in a 64-bit integer. The idea is to compute $a \cdot b \pmod m$ without using bignum arithmetics.
211
215
212
216
**Solution:** We simply apply the binary construction algorithm described above, only performing additions instead of multiplications. In other words, we have "expanded" the multiplication of two numbers to $O (\log m)$ operations of addition and multiplication by two (which, in essence, is an addition).
213
217
214
218
$$a \cdot b = \begin{cases}
215
-
0 &\text{if }a = 0 \\\\
216
-
2 \cdot \frac{a}{2} \cdot b &\text{if }a > 0 \text{ and }a \text{ even} \\\\
219
+
0 &\text{if }a = 0 \\
220
+
2 \cdot \frac{a}{2} \cdot b &\text{if }a > 0 \text{ and }a \text{ even} \\
217
221
2 \cdot \frac{a-1}{2} \cdot b + b &\text{if }a > 0 \text{ and }a \text{ odd}
Copy file name to clipboardExpand all lines: src/algebra/euclid-algorithm.md
+14-4Lines changed: 14 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,15 @@
1
-
<!--?title Euclidean algorithm for computing the greatest common divisor -->
1
+
---
2
+
title: Euclidean algorithm for computing the greatest common divisor
3
+
hide:
4
+
- navigation
5
+
---
2
6
# Euclidean algorithm for computing the greatest common divisor
3
7
4
8
Given two non-negative integers $a$ and $b$, we have to find their **GCD** (greatest common divisor), i.e. the largest number which is a divisor of both $a$ and $b$.
5
9
It's commonly denoted by $\gcd(a, b)$. Mathematically it is defined as:
10
+
6
11
$$\gcd(a, b) = \max_ {k = 1 \dots \infty ~ : ~ k \mid a ~ \wedge k ~ \mid b} k.$$
12
+
7
13
(here the symbol "$\mid$" denotes divisibility, i.e. "$k \mid a$" means "$k$ divides $a$")
8
14
9
15
When one of the numbers is zero, while the other is non-zero, their greatest common divisor, by definition, is the second number. When both numbers are zero, their greatest common divisor is undefined (it can be any arbitrarily large number), but we can define it to be zero. Which gives us a simple rule: if one of the numbers is zero, the greatest common divisor is the other number.
@@ -16,7 +22,7 @@ The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it
16
22
17
23
The algorithm is extremely simple:
18
24
19
-
$$\gcd(a, b) = \begin{cases}a,&\text{if }b = 0 \\\\ \gcd(b, a \bmod b),&\text{otherwise.}\end{cases}$$
25
+
$$\gcd(a, b) = \begin{cases}a,&\text{if }b = 0 \\ \gcd(b, a \bmod b),&\text{otherwise.}\end{cases}$$
20
26
21
27
## Implementation
22
28
@@ -60,12 +66,15 @@ We will show that the value on the left side of the equation divides the value o
60
66
Let $d = \gcd(a, b)$. Then by definition $d\mid a$ and $d\mid b$.
61
67
62
68
Now let's represent the remainder of the division of $a$ by $b$ as follows:
69
+
63
70
$$a \bmod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor$$
64
71
65
72
From this it follows that $d \mid (a \bmod b)$, which means we have the system of divisibilities:
66
-
$$\begin{cases}d \mid b,\\\\ d \mid (a \mod b)\end{cases}$$
73
+
74
+
$$\begin{cases}d \mid b,\\ d \mid (a \mod b)\end{cases}$$
67
75
68
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
+
69
78
$$d = \gcd(a, b) \mid \gcd(b, a \mod b)$$
70
79
71
80
Thus we have shown that the left side of the original equation divides the right. The second half of the proof is similar.
@@ -83,6 +92,7 @@ Given that Fibonacci numbers grow exponentially, we get that the Euclidean algor
83
92
## Least common multiple
84
93
85
94
Calculating the least common multiple (commonly denoted **LCM**) can be reduced to calculating the GCD with the following simple formula:
95
+
86
96
$$\text{lcm}(a, b) = \frac{a \cdot b}{\gcd(a, b)}$$
87
97
88
98
Thus, LCM can be calculated using the Euclidean algorithm with the same time complexity:
@@ -128,7 +138,7 @@ int gcd(int a, int b) {
128
138
```
129
139
130
140
Notice, that such an optimization is usually not necessary, and most programming languages already have a GCD function in their standard libraries.
131
-
E.g. C++17 has such a function in the `numeric` header.
141
+
E.g. C++17 has such a function `std::gcd` in the `numeric` header.
Copy file name to clipboardExpand all lines: src/algebra/extended-euclid-algorithm.md
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<!--?title Extended Euclidean Algorithm -->
1
+
---
2
+
title: Extended Euclidean Algorithm
3
+
hide:
4
+
- navigation
5
+
---
2
6
# Extended Euclidean Algorithm
3
7
4
8
While the [Euclidean algorithm](euclid-algorithm.md) calculates only the greatest common divisor (GCD) of two integers $a$ and $b$, the extended version also finds a way to represent GCD in terms of $a$ and $b$, i.e. coefficients $x$ and $y$ for which:
@@ -44,13 +48,13 @@ $$g = a \cdot y_1 + b \cdot \left( x_1 - y_1 \cdot \left\lfloor \frac{a}{b} \rig
44
48
We found the values of $x$ and $y$:
45
49
46
50
$$\begin{cases}
47
-
x = y_1 \\\\
51
+
x = y_1 \\
48
52
y = x_1 - y_1 \cdot \left\lfloor \frac{a}{b} \right\rfloor
49
53
\end{cases} $$
50
54
51
55
## Implementation
52
56
53
-
```cpp extended_gcd
57
+
```{.cppfile=extended_gcd}
54
58
intgcd(int a, int b, int& x, int& y) {
55
59
if (b == 0) {
56
60
x = 1;
@@ -74,7 +78,7 @@ This implementation of extended Euclidean algorithm produces correct results for
74
78
It's also possible to write the Extended Euclidean algorithm in an iterative way.
75
79
Because it avoids recursion, the code will run a little bit faster than the recursive one.
Copy file name to clipboardExpand all lines: src/algebra/fft.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,6 @@
1
-
<!--?title Fast Fourier transform -->
1
+
---
2
+
title: Fast Fourier transform
3
+
---
2
4
# Fast Fourier transform
3
5
4
6
In this article we will discuss an algorithm that allows us to multiply two polynomials of length $n$ in $O(n \log n)$ time, which is better than the trivial multiplication which takes $O(n^2)$ time.
Obviously, this process can be repeated again, so all the numbers of the form:
83
88
84
89
$$x = x_0 + k \cdot \frac{b}{g}$$
90
+
85
91
$$y = y_0 - k \cdot \frac{a}{g}$$
86
92
87
93
are solutions of the given Diophantine equation.
@@ -109,7 +115,7 @@ Following is the code implementing this idea.
109
115
Notice that we divide $a$ and $b$ at the beginning by $g$.
110
116
Since the equation $a x + b y = c$ is equivalent to the equation $\frac{a}{g} x + \frac{b}{g} y = \frac{c}{g}$, we can use this one instead and have $\gcd(\frac{a}{g}, \frac{b}{g}) = 1$, which simplifies the formulas.
111
117
112
-
```cpp linear_diophantine_all
118
+
```{.cpp file=linear_diophantine_all}
113
119
void shift_solution(int & x, int & y, int a, int b, int cnt) {
114
120
x += cnt * b;
115
121
y -= cnt * a;
@@ -162,7 +168,7 @@ int find_all_solutions(int a, int b, int c, int minx, int maxx, int miny, int ma
162
168
163
169
Once we have $l_x$ and $r_x$, it is also simple to enumerate through all the solutions. Just need to iterate through $x = l_x + k \cdot \frac{b}{g}$ for all $k \ge 0$ until $x = r_x$, and find the corresponding $y$ values using the equation $a x + b y = c$.
164
170
165
-
## Find the solution with minimum value of $x + y$
171
+
## Find the solution with minimum value of $x + y$ { data-toc-label='Find the solution with minimum value of <scripttype="math/tex">x + y</script>' }
166
172
167
173
Here, $x$ and $y$ also need to be given some restriction, otherwise, the answer may become negative infinity.
168
174
@@ -171,6 +177,7 @@ The idea is similar to previous section: We find any solution of the Diophantine
171
177
Finally, use the knowledge of the set of all solutions to find the minimum:
0 commit comments