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/all-submasks.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ Let's prove that the inner loop will execute a total of $O(3^n)$ iterations.
60
60
61
61
As there are a total of $n$ bits, there will be $3^n$ different combinations.
62
62
63
-
**Second proof**: Note that if mask $m$ has $k$ enabled bits, then it will have $2^k$ submasks. As we have a total of $\binom{n}{k}$ masks with $k$ enabled bits (see [binomial coefficients](./combinatorics/binomial-coefficients.html)), then the total number of combinations for all masks will be:
63
+
**Second proof**: Note that if mask $m$ has $k$ enabled bits, then it will have $2^k$ submasks. As we have a total of $\binom{n}{k}$ masks with $k$ enabled bits (see [binomial coefficients](../combinatorics/binomial-coefficients.md)), then the total number of combinations for all masks will be:
Copy file name to clipboardExpand all lines: src/algebra/big-integer.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -162,9 +162,9 @@ This method is often used for calculations modulo non-prime number M; in this ca
162
162
163
163
The idea is to choose a set of prime numbers (typically they are small enough to fit into standard integer data type) and to store an integer as a vector of remainders from division of the integer by each of those primes.
164
164
165
-
Chinese remainder theorem states that this representation is sufficient to uniquely restore any number from 0 to product of these primes minus one. [Garner algorithm](./algebra/chinese-remainder-theorem.html) allows to restore the number from such representation to normal integer.
165
+
Chinese remainder theorem states that this representation is sufficient to uniquely restore any number from 0 to product of these primes minus one. [Garner algorithm](chinese-remainder-theorem.md) allows to restore the number from such representation to normal integer.
166
166
167
-
This method allows to save memory compared to the classical approach (though the savings are not as dramatic as in factorization representation). Besides, it allows to perform fast addition, subtraction and multiplication in time proportional to the number of prime numbers used as modulos (see [Chinese remainder theorem](./algebra/chinese-remainder-theorem.html) article for implementation).
167
+
This method allows to save memory compared to the classical approach (though the savings are not as dramatic as in factorization representation). Besides, it allows to perform fast addition, subtraction and multiplication in time proportional to the number of prime numbers used as modulos (see [Chinese remainder theorem](chinese-remainder-theorem.md) article for implementation).
168
168
169
169
The tradeoff is that converting the integer back to normal form is rather laborious and requires implementing classical arbitrary-precision arithmetic with multiplication. Besides, this method doesn't support division.
Copy file name to clipboardExpand all lines: src/algebra/binary-exp.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,7 @@ long long binpow(long long a, long long b) {
87
87
88
88
**Problem:**
89
89
Compute $x^n \bmod m$.
90
-
This is a very common operation. For instance it is used in computing the [modular multiplicative inverse](./algebra/module-inverse.html).
90
+
This is a very common operation. For instance it is used in computing the [modular multiplicative inverse](module-inverse.md).
91
91
92
92
**Solution:**
93
93
Since we know that the module operator doesn't interfere with multiplications ($a \cdot b \equiv (a \bmod m) \cdot (b \bmod m) \pmod m$), we can directly use the same code, and just replace every multiplication with a modular multiplication:
@@ -107,13 +107,13 @@ long long binpow(long long a, long long b, long long m) {
107
107
```
108
108
109
109
**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$.
110
-
This follows directly from [Fermat's little theorem](./algebra/module-inverse.html#toc-tgt-2).
110
+
This follows directly from [Fermat's little theorem](module-inverse.md#toc-tgt-2).
111
111
112
112
### Effective computation of Fibonacci numbers
113
113
114
114
**Problem:** Compute $n$-th Fibonacci number $F_n$.
115
115
116
-
**Solution:** For more details, see the [Fibonacci Number article](./algebra/fibonacci-numbers.html).
116
+
**Solution:** For more details, see the [Fibonacci Number article](fibonacci-numbers.md).
117
117
We will only go through an overview of the algorithm.
118
118
To compute the next Fibonacci number, only the two previous ones are needed, as $F_n = F_{n-1} + F_{n-2}$.
119
119
We can build a $2 \times 2$ matrix that describes this transformation:
@@ -194,7 +194,7 @@ Now, once every transformation is described as a matrix, the sequence of transfo
194
194
195
195
**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$.
196
196
197
-
**Solution:** This problem is considered in more detail in [a separate article](./graph/fixed_length_paths.html). The algorithm consists of raising the adjacency matrix $M$ of the graph (a matrix where $m_{ij} = 1$ if there is an edge from $i$ to $j$, or $0$ otherwise) to the $k$-th power. Now $m_{ij}$ will be the number of paths of length $k$ from $i$ to $j$. The time complexity of this solution is $O(n^3 \log k)$.
197
+
**Solution:** This problem is considered in more detail in [a separate article](../graph/fixed_length_paths.md). The algorithm consists of raising the adjacency matrix $M$ of the graph (a matrix where $m_{ij} = 1$ if there is an edge from $i$ to $j$, or $0$ otherwise) to the $k$-th power. Now $m_{ij}$ will be the number of paths of length $k$ from $i$ to $j$. The time complexity of this solution is $O(n^3 \log k)$.
198
198
199
199
**Note:** In that same article, another variation of this problem is considered: when the edges are weighted and it is required to find the minimum weight path containing exactly $k$ edges. As shown in that article, this problem is also solved by exponentiation of the adjacency matrix. The matrix would have the weight of the edge from $i$ to $j$, or $\infty$ if there is no such edge.
200
200
Instead of the usual operation of multiplying two matrices, a modified one should be used:
Copy file name to clipboardExpand all lines: src/algebra/chinese-remainder-theorem.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ Let $r_{ij}$ denote the inverse of $p_i$ modulo $p_j$
44
44
$$
45
45
r_{ij} = (p_i)^{-1} \pmod{p_j}
46
46
$$
47
-
which can be found using the algorithm described in [Modular Inverse](./algebra/module-inverse.html). Substituting $a$ from the mixed radix representation into the first congruence equation we obtain
47
+
which can be found using the algorithm described in [Modular Inverse](module-inverse.md). Substituting $a$ from the mixed radix representation into the first congruence equation we obtain
Copy file name to clipboardExpand all lines: src/algebra/discrete-log.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ This problem can be solved using the meet-in-the-middle method as follows:
43
43
44
44
## Complexity
45
45
46
-
We can calculate $f_1(p)$ in $O(\log m)$ using the [binary exponentation algorithm](./algebra/binary-exp.html). Similarly for $f_2(q)$.
46
+
We can calculate $f_1(p)$ in $O(\log m)$ using the [binary exponentation algorithm](binary-exp.md). Similarly for $f_2(q)$.
47
47
48
48
In the first step of the algorithm, we need to calculate $f_1$ for every possible argument $p$ and then sort the values. Thus, this step has complexity:
49
49
@@ -125,7 +125,7 @@ With this change, the complexity of the algorithm is still the same, but now the
125
125
Instead of a `map`, we can also use a hash table (`unordered_map` in C++) which has the average time complexity $O(1)$ for inserting and searching.
126
126
127
127
Problems often ask for the minimum $x$ which satisfies the solution.
128
-
It is possible to get all answers and take the minimum, or reduce the first found answer using [Euler's theorem](./algebra/phi-function.html#toc-tgt-2), but we can be smart about the order in which we calculate values and ensure the first answer we find is the minimum.
128
+
It is possible to get all answers and take the minimum, or reduce the first found answer using [Euler's theorem](phi-function.md#toc-tgt-2), but we can be smart about the order in which we calculate values and ensure the first answer we find is the minimum.
129
129
130
130
```cpp discrete_log
131
131
// Returns minimum x for which a ^ x % m = b % m, a and m are coprime.
Copy file name to clipboardExpand all lines: src/algebra/discrete-root.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ $x^k \equiv a \pmod n$
8
8
9
9
## The algorithm
10
10
11
-
We will solve this problem by reducing it to the [discrete logarithm problem](./algebra/discrete-log.html).
11
+
We will solve this problem by reducing it to the [discrete logarithm problem](discrete-log.md).
12
12
13
-
Let's apply the concept of a [primitive root](./algebra/primitive-root.html) modulo $n$. Let $g$ be a primitive root modulo $n$. Note that since $n$ is prime, it must exist, and it can be found in $O(Ans \cdot \log \phi (n) \cdot \log n) = O(Ans \cdot \log^2 n)$ plus time of factoring $\phi (n)$.
13
+
Let's apply the concept of a [primitive root](primitive-root.md) modulo $n$. Let $g$ be a primitive root modulo $n$. Note that since $n$ is prime, it must exist, and it can be found in $O(Ans \cdot \log \phi (n) \cdot \log n) = O(Ans \cdot \log^2 n)$ plus time of factoring $\phi (n)$.
14
14
15
15
We can easily discard the case where $a = 0$. In this case, obviously there is only one answer: $x = 0$.
Copy file name to clipboardExpand all lines: src/algebra/extended-euclid-algorithm.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
<!--?title Extended Euclidean Algorithm -->
2
2
# Extended Euclidean Algorithm
3
3
4
-
While the [Euclidean algorithm](./algebra/euclid-algorithm.html) 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:
4
+
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:
5
5
6
6
$$a \cdot x + b \cdot y = \gcd(a, b)$$
7
7
8
8
It's important to note, that we can always find such a representation, for instance $\gcd(55, 80) = 5$ therefore we can represent $5$ as a linear combination with the terms $55$ and $80$: $55 \cdot 3 + 80 \cdot (-2) = 5$
9
9
10
-
A more general form of that problem is discussed in the article about [Linear Diophantine Equations](algebra/linear-diophantine-equation.html).
10
+
A more general form of that problem is discussed in the article about [Linear Diophantine Equations](linear-diophantine-equation.md).
11
11
It will build upon this algorithm.
12
12
13
13
## Algorithm
@@ -88,7 +88,7 @@ int gcd(int a, int b, int& x, int& y) {
88
88
}
89
89
```
90
90
91
-
If you look closely at the variable `a1` and `b1`, you can notice that they taking exactly the same values as in the iterative version of the normal [Euclidean algorithm](algebra/euclid-algorithm.html). So the algorithm will at least compute the correct GCD.
91
+
If you look closely at the variable `a1` and `b1`, you can notice that they taking exactly the same values as in the iterative version of the normal [Euclidean algorithm](euclid-algorithm.md). So the algorithm will at least compute the correct GCD.
92
92
93
93
To see why the algorithm also computes the correct coefficients, you can check that the following invariants will hold at any time (before the while loop, and at the end of each iteration): $x \cdot a + y \cdot b = a_1$ and $x_1 \cdot a + y_1 \cdot b = b_1$.
94
94
It's trivial to see, that these two equations are satisfied at the beginning.
Learning how to effectively calculate this modified factorial allows us to quickly calculate the value of the various combinatorial formulas (for example, [Binomial coefficients](./combinatorics/binomial-coefficients.html)).
15
+
Learning how to effectively calculate this modified factorial allows us to quickly calculate the value of the various combinatorial formulas (for example, [Binomial coefficients](../combinatorics/binomial-coefficients.md)).
16
16
17
17
## Algorithm
18
18
Let's write this modified factorial explicitly.
@@ -35,7 +35,7 @@ The main part of the blocks it is easy to count — it's just $(p-1)!\ \mathrm{m
35
35
We can compute that programmatically or just apply Wilson theorem which states that $(p-1)! \bmod p = -1$ for any prime $p$.
36
36
37
37
We have exactly $\lfloor \frac{n}{p} \rfloor$ such blocks, therefore we need to raise $-1$ to the power of $\lfloor \frac{n}{p} \rfloor$.
38
-
This can be done in logarithmic time using [Binary Exponentiation](./algebra/binary-exp.html); however you can also notice that the result will switch between $-1$ and $1$, so we only need to look at the parity of the exponent and multiply by $-1$ if the parity is odd.
38
+
This can be done in logarithmic time using [Binary Exponentiation](binary-exp.md); however you can also notice that the result will switch between $-1$ and $1$, so we only need to look at the parity of the exponent and multiply by $-1$ if the parity is odd.
39
39
And instead of a multiplication, we can also just subtract the current result from $p$.
40
40
41
41
The value of the last partial block can be calculated separately in $O(p)$.
Copy file name to clipboardExpand all lines: src/algebra/factorization.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
In this article we list several algorithms for factorizing integers, each of them can be both fast and also slow (some slower than others) depending on their input.
5
5
6
6
Notice, if the number that you want to factorize is actually a prime number, most of the algorithms, especially Fermat's factorization algorithm, Pollard's p-1, Pollard's rho algorithm will run very slow.
7
-
So it makes sense to perform a probabilistic (or a fast deterministic) [primality test](./algebra/primality_tests.html) before trying to factorize the number.
7
+
So it makes sense to perform a probabilistic (or a fast deterministic) [primality test](primality_tests.md) before trying to factorize the number.
8
8
9
9
## Trial division
10
10
@@ -103,7 +103,7 @@ However, also the skip lists will get a lot bigger.
103
103
### Precomputed primes
104
104
105
105
Extending the wheel factorization with more and more primes will leave exactly the primes to check.
106
-
So a good way of checking is just to precompute all prime numbers with the [Sieve of Eratosthenes](./algebra/sieve-of-eratosthenes.html) until $\sqrt{n}$ and test them individually.
106
+
So a good way of checking is just to precompute all prime numbers with the [Sieve of Eratosthenes](sieve-of-eratosthenes.md) until $\sqrt{n}$ and test them individually.
107
107
108
108
```cpp factorization_trial_division4
109
109
vector<long long> primes;
@@ -161,7 +161,7 @@ E.g. the prime factorization of $4817191$ is $1303 \cdot 3697$.
161
161
And the factors are $31$-powersmooth and $16$-powersmooth respectably, because $1303 - 1 = 2 \cdot 3 \cdot 7 \cdot 31$ and $3697 - 1 = 2^4 \cdot 3 \cdot 7 \cdot 11$.
162
162
In 1974 John Pollard invented a method to extracts $B$-powersmooth factors from a composite number.
163
163
164
-
The idea comes from [Fermat's little theorem](./algebra/phi-function.html#application).
164
+
The idea comes from [Fermat's little theorem](phi-function.md#application).
165
165
Let a factorization of $n$ be $n = p \cdot q$.
166
166
It says that if $a$ is coprime to $p$, the following statement holds:
We don't know $p$ yet, so how can we argue about the sequence $\{x_i \bmod p\}$?
@@ -328,7 +328,7 @@ i & x_i \bmod n & x_{2i} \bmod n & x_i \bmod 317 & x_{2i} \bmod 317 & \gcd(x_i -
328
328
\end{array}$$
329
329
330
330
The implementation uses a function `mult`, that multiplies two integers $\le 10^{18}$ without overflow by using a GCC's type `__int128` for 128-bit integer.
331
-
If GCC is not available, you can using a similar idea as [binary exponentiation](./algebra/binary-exp.html).
331
+
If GCC is not available, you can using a similar idea as [binary exponentiation](binary-exp.md).
332
332
333
333
```cpp pollard_rho_mult2
334
334
long long mult(long long a, long long b, long long mod) {
@@ -343,7 +343,7 @@ long long mult(long long a, long long b, long long mod) {
343
343
}
344
344
```
345
345
346
-
Alternatively you can also implement the [Montgomery multiplication](./algebra/montgomery_multiplication.html).
346
+
Alternatively you can also implement the [Montgomery multiplication](montgomery_multiplication.md).
347
347
348
348
As already noticed above: if $n$ is composite and the algorithm returns $n$ as factor, you have to repeat the procedure with different parameter $x_0$ and $c$.
349
349
E.g. the choice $x_0 = c = 1$ will not factor $25 = 5 \cdot 5$.
Here the function `inverse` computes the modular inverse (see [Modular Multiplicative Inverse](./algebra/module-inverse.html)).
485
+
Here the function `inverse` computes the modular inverse (see [Modular Multiplicative Inverse](module-inverse.md)).
486
486
The constants `mod`, `root`, `root_pw` determine the module and the root, and `root_1` is the inverse of `root` modulo `mod`.
487
487
488
488
In practice this implementation is slower than the implementation using complex numbers (due to the huge number of modulo operations), but it has some advantages such as less memory usage and no rounding errors.
@@ -494,7 +494,7 @@ Multiplying two polynomial $A(x)$ and $B(x)$, and computing the coefficients mod
494
494
The number theoretic transform only works for certain prime numbers.
495
495
What about the case when the modulus is not of the desired form?
496
496
497
-
One option would be to perform multiple number theoretic transforms with different prime numbers of the form $c 2^k + 1$, then apply the [Chinese Remainder Theorem](./algebra/chinese-remainder-theorem.html) to compute the final coefficients.
497
+
One option would be to perform multiple number theoretic transforms with different prime numbers of the form $c 2^k + 1$, then apply the [Chinese Remainder Theorem](chinese-remainder-theorem.md) to compute the final coefficients.
498
498
499
499
Another options is to distribute the polynomials $A(x)$ and $B(x)$ into two smaller polynomials each
0 commit comments