Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41d1df2

Browse files
committed
fixes
1 parent 093d331 commit 41d1df2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+380
-230
lines changed

src/algebra/factorial-divisors.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
<!--?title Finding Power of Factorial Divisor -->
2-
1+
---
2+
title: Finding Power of Factorial Divisor
3+
hide:
4+
- navigation
5+
---
36
# Finding Power of Factorial Divisor
47

58
You are given two numbers $n$ and $k$. Find the largest power of $k$ $x$ such that $n!$ is divisible by $k^x$.

src/combinatorics/binomial-coefficients.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,36 @@ Note that for $n \lt k$ the value of $\binom n k$ is assumed to be zero.
3434
Binomial coefficients have many different properties. Here are the simplest of them:
3535

3636
* Symmetry rule:
37-
$$ \binom n k = \binom n {n-k} $$
37+
38+
\[ \binom n k = \binom n {n-k} \]
39+
3840
* Factoring in:
39-
$$ \binom n k = \frac n k \binom {n-1} {k-1} $$
41+
42+
\[ \binom n k = \frac n k \binom {n-1} {k-1} \]
43+
4044
* Sum over $k$:
41-
$$ \sum_{k = 0}^n \binom n k = 2 ^ n $$
45+
46+
\[ \sum_{k = 0}^n \binom n k = 2 ^ n \]
47+
4248
* Sum over $n$:
43-
$$ \sum_{m = 0}^n \binom m k = \binom {n + 1} {k + 1} $$
49+
50+
\[ \sum_{m = 0}^n \binom m k = \binom {n + 1} {k + 1} \]
51+
4452
* Sum over $n$ and $k$:
45-
$$ \sum_{k = 0}^m \binom {n + k} k = \binom {n + m + 1} m $$
53+
54+
\[ \sum_{k = 0}^m \binom {n + k} k = \binom {n + m + 1} m \]
55+
4656
* Sum of the squares:
47-
$$ {\binom n 0}^2 + {\binom n 1}^2 + \cdots + {\binom n n}^2 = \binom {2n} n $$
57+
58+
\[ {\binom n 0}^2 + {\binom n 1}^2 + \cdots + {\binom n n}^2 = \binom {2n} n \]
59+
4860
* Weighted sum:
49-
$$ 1 \binom n 1 + 2 \binom n 2 + \cdots + n \binom n n = n 2^{n-1} $$
61+
62+
\[ 1 \binom n 1 + 2 \binom n 2 + \cdots + n \binom n n = n 2^{n-1} \]
63+
5064
* Connection with the [Fibonacci numbers](../algebra/fibonacci-numbers.md):
51-
$$ \binom n 0 + \binom {n-1} 1 + \cdots + \binom {n-k} k + \cdots + \binom 0 n = F_{n+1} $$
65+
66+
\[ \binom n 0 + \binom {n-1} 1 + \cdots + \binom {n-k} k + \cdots + \binom 0 n = F_{n+1} \]
5267

5368
## Calculation
5469

@@ -100,25 +115,28 @@ for (int n = 1; n <= maxn; ++n) {
100115

101116
If the entire table of values is not necessary, storing only two last rows of it is sufficient (current $n$-th row and the previous $n-1$-th).
102117

103-
### Calculation in $O(1)$
118+
### Calculation in $O(1)$ {data-toc-label="Calculation in O(1)"}
104119

105120
Finally, in some situations it is beneficial to precompute all the factorials in order to produce any necessary binomial coefficient with only two divisions later. This can be advantageous when using [long arithmetic](../algebra/big-integer.md), when the memory does not allow precomputation of the whole Pascal's triangle.
106121

107122

108-
## Computing binomial coefficients modulo $m$.
123+
## Computing binomial coefficients modulo $m$ {data-toc-label="Computing binomial coefficients modulo m"}
109124

110125
Quite often you come across the problem of computing binomial coefficients modulo some $m$.
111126

112-
### Binomial coefficient for small $n$
127+
### Binomial coefficient for small $n$ {data-toc-label="Binomial coefficient for small n"}
113128

114129
The previously discussed approach of Pascal's triangle can be used to calculate all values of $\binom{n}{k} \bmod m$ for reasonably small $n$, since it requires time complexity $\mathcal{O}(n^2)$. This approach can handle any modulo, since only addition operations are used.
115130

116131

117132
### Binomial coefficient modulo large prime
118133

119134
The formula for the binomial coefficients is
135+
120136
$$\binom n k = \frac {n!} {k!(n-k)!},$$
137+
121138
so if we want to compute it modulo some prime $m > n$ we get
139+
122140
$$\binom n k \equiv n! \cdot (k!)^{-1} \cdot ((n-k)!)^{-1} \mod m.$$
123141

124142
First we precompute all factorials modulo $m$ up to $\text{MAXN}!$ in $O(\text{MAXN})$ time.
@@ -158,6 +176,7 @@ We compute for each $x!$ the biggest exponent $c$ such that $p^c$ divides $x!$,
158176
Let $c(x)$ be that number.
159177
And let $g(x) := \frac{x!}{p^{c(x)}}$.
160178
Then we can write the binomial coefficient as:
179+
161180
$$\binom n k = \frac {g(n) p^{c(n)}} {g(k) p^{c(k)} g(n-k) p^{c(n-k)}} = \frac {g(n)} {g(k) g(n-k)}p^{c(n) - c(k) - c(n-k)}$$
162181

163182
The interesting thing is, that $g(x)$ is now free from the prime divisor $p$.
@@ -177,7 +196,7 @@ We can compute the binomial coefficient modulo $p_i^{e_i}$ for every $i$.
177196
This gives us $h$ different congruences.
178197
Since all moduli $p_i^{e_i}$ are coprime, we can apply the [Chinese Remainder Theorem](../algebra/chinese-remainder-theorem.md) to compute the binomial coefficient modulo the product of the moduli, which is the desired binomial coefficient modulo $m$.
179198

180-
### Binomial coefficient for large $n$ and small modulo
199+
### Binomial coefficient for large $n$ and small modulo {data-toc-label="Binomial coefficient for large n and small modulo"}
181200

182201
When $n$ is too large, the $\mathcal{O}(n)$ algorithms discussed above become impractical. However, if the modulo $m$ is small there are still ways to calculate $\binom{n}{k} \bmod m$.
183202

src/combinatorics/bracket_sequences.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ Otherwise it is.
4545

4646
The number of balanced bracket sequences with only one bracket type can be calculated using the [Catalan numbers](catalan-numbers.md).
4747
The number of balanced bracket sequences of length $2n$ ($n$ pairs of brackets) is:
48+
4849
$$\frac{1}{n+1} \binom{2n}{n}$$
4950

5051
If we allow $k$ types of brackets, then each pair be of any of the $k$ types (independently of the others), thus the number of balanced bracket sequences is:
52+
5153
$$\frac{1}{n+1} \binom{2n}{n} k^n$$
5254

5355
### Dynamic programming
@@ -59,7 +61,9 @@ And somewhere later is the corresponding closing bracket of the pair.
5961
It is clear that inside this pair there is a balanced bracket sequence, and similarly after this pair there is a balanced bracket sequence.
6062
So to compute $d[n]$, we will look at how many balanced sequences of $i$ pairs of brackets are inside this first bracket pair, and how many balanced sequences with $n-1-i$ pairs are after this pair.
6163
Consequently the formula has the form:
64+
6265
$$d[n] = \sum_{i=0}^{n-1} d[i] \cdot d[n-1-i]$$
66+
6367
The initial value for this recurrence is $d[0] = 1$.
6468

6569
## Finding the lexicographical next balanced sequence
@@ -79,7 +83,7 @@ We change the symbol, compute the number of opening and closing brackets that we
7983

8084
If we find do suitable position, then this sequence is already the maximal possible one, and there is no answer.
8185

82-
```cpp next_balanced_brackets_sequence
86+
```{.cpp file=next_balanced_brackets_sequence}
8387
bool next_balanced_sequence(string & s) {
8488
int n = s.size();
8589
int depth = 0;
@@ -128,7 +132,9 @@ For the start value $i = 0$ the answer is obvious: $d[0][0] = 1$, and $d[0][j] =
128132
Now let $i > 0$, and we look at the last character in the sequence.
129133
If the last character was an opening bracket $($, then the state before was $(i-1, j-1)$, if it was a closing bracket $)$, then the previous state was $(i-1, j+1)$.
130134
Thus we obtain the recursion formula:
135+
131136
$$d[i][j] = d[i-1][j-1] + d[i-1][j+1]$$
137+
132138
$d[i][j] = 0$ holds obviously for negative $j$.
133139
Thus we can compute this array in $O(n^2)$.
134140
@@ -142,14 +148,16 @@ If the current character $s[i]$ is equal to $)$, then we must add $d[2n-i-1][\te
142148
New let there be $k$ different bracket types.
143149
144150
Thus, when we look at the current character $s[i]$ before recomputing $\text{depth}$, we have to go through all bracket types that are smaller than the current character, and try to put this bracket into the current position (obtaining a new balance $\text{ndepth} = \text{depth} \pm 1$), and add the number of ways to finish the sequence (length $2n-i-1$, balance $ndepth$) to the answer:
151+
145152
$$d[2n - i - 1][\text{ndepth}] \cdot k^{\frac{2n - i - 1 - ndepth}{2}}$$
153+
146154
This formula can be derived as follows:
147155
First we "forget" that there are multiple bracket types, and just take the answer $d[2n - i - 1][\text{ndepth}]$.
148156
Now we consider how the answer will change is we have $k$ types of brackets.
149157
We have $2n - i - 1$ undefined positions, of which $\text{ndepth}$ are already predetermined because of the opening brackets.
150158
But all the other brackets ($(2n - i - i - \text{ndepth})/2$ pairs) can be of any type, therefore we multiply the number by such a power of $k$.
151159
152-
## Finding the $k$-th sequence
160+
## Finding the $k$-th sequence {data-toc-label="Finding the k-th sequence"}
153161
154162
Let $n$ be the number of bracket pairs in the sequence.
155163
We have to find the $k$-th balanced sequence in lexicographically sorted list of all balanced sequences for a given $k$.
@@ -165,7 +173,7 @@ To have to put an opening bracket character, it $d[2n - i - 1][\text{depth}+1] \
165173
We increment the counter $\text{depth}$, and move on to the next character.
166174
Otherwise we decrement $k$ by $d[2n - i - 1][\text{depth}+1]$, put a closing bracket and move on.
167175
168-
```cpp kth_balances_bracket
176+
```{.cpp file=kth_balances_bracket}
169177
string kth_balanced(int n, int k) {
170178
vector<vector<int>> d(2*n+1, vector<int>(n+1, 0));
171179
d[0][0] = 1;
@@ -198,7 +206,7 @@ The solution will only differ slightly in that we have to multiply the value $d[
198206

199207
Here is an implementation using two types of brackets: round and square:
200208

201-
```cpp kth_balances_bracket_multiple
209+
```{.cpp file=kth_balances_bracket_multiple}
202210
string kth_balanced2(int n, int k) {
203211
vector<vector<int>> d(2*n+1, vector<int>(n+1, 0));
204212
d[0][0] = 1;

src/combinatorics/burnside.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ At the same time we introduce a partition of this set into equivalence classes.
3636

3737
For example, suppose $n = 3$, and the tree consists of the root $1$ and its two children $2$ and $3$.
3838
Then the following functions $f_1$ and $f_2$ are considered equivalent.
39-
$$\begin{array}{ll}f_1(1) = 0 & f_2(1) = 0\\\\
40-
f_1(2) = 1 & f_2(2) = 0\\\\
39+
40+
$$\begin{array}{ll}
41+
f_1(1) = 0 & f_2(1) = 0\\
42+
f_1(2) = 1 & f_2(2) = 0\\
4143
f_1(3) = 0 & f_2(3) = 1
4244
\end{array}$$
4345

@@ -47,6 +49,7 @@ Why do these two function $f_1$ and $f_2$ belong to the same equivalence class?
4749
Intuitively this is understandable - we can rearrange the children of vertex $1$, the vertices $2$ and $3$, and after such a transformation of the function $f_1$ it will coincide with $f_2$.
4850

4951
But formally this means that there exists an **invariant permutation** $\pi$ (i.e. a permutation which does not change the object itself, but only its representation), such that:
52+
5053
$$f_2 \pi \equiv f_1$$
5154

5255
So starting from the definition of objects, we can find all the invariant permutations, i.e. all permutations which do not change the object when applying the permutation to the representation.
@@ -69,6 +72,7 @@ We denote by $I(\pi)$ the **number of fixed points** for the permutation $\pi$.
6972

7073
Then **Burnside's lemma** goes as follows:
7174
the number of equivalence classes is equal to the sum of the numbers of fixed points with respect to all permutations from the group $G$, divided by the size of this group:
75+
7276
$$|\text{Classes}| = \frac{1}{|G|} \sum_{\pi \in G} I(\pi)$$
7377

7478
Although Burnside's lemma itself is not so convenient to use in practice (it is unclear how to quickly look for the value $I(\pi)$, it most clearly reveals the mathematical essence on which the idea of calculating equivalence classes is based.
@@ -81,11 +85,13 @@ The proof here is the simplest known, and does not use group theory.
8185
The proof was published by Kenneth P. Bogart in 1991.
8286

8387
We need to prove the following statement:
88+
8489
$$|\text{Classes}| \cdot |G| = \sum_{\pi \in G} I(\pi)$$
8590

8691
The value on the right side is nothing more than the number of "invariant pairs" $(f, \pi)$, i.e. pairs such that $f \pi \equiv f$.
8792
It is obvious that we can change the order of summation.
8893
We let the sum iterate over all elements $f$ and sum over the values $J(f)$ - the number of permutations for which $f$ is a fixed point.
94+
8995
$$|\text{Classes}| \cdot |G| = \sum_{f} J(f)$$
9096

9197
To prove this formula we will compose a table with columns labeled with all functions $f_i$ and rows labeled with all permutations $\pi_j$.
@@ -107,6 +113,7 @@ On the other hand, all columns within the same equivalence class are the same as
107113
Therefore within each column of a given equivalence class any element $g$ occurs exactly $J(g)$ times.
108114

109115
Thus if we arbitrarily take one column from each equivalence class, and sum the number of elements in them, we obtain on one hand $|\text{Classes}| \cdot |G|$ (simply by multiplying the number of columns by the number of rows), and on the other hand the sum of the quantities $J(f)$ for all $f$ (this follows from all the previous arguments):
116+
110117
$$|\text{Classes}| \cdot |G| = \sum_{f} J(f)$$
111118

112119
## Pólya enumeration theorem
@@ -120,7 +127,9 @@ The general formula of the theorem will not be discussed.
120127

121128
We denote by $C(\pi)$ the number of cycles in the permutation $\pi$.
122129
Then the following formula (a **special case of the Pólya enumeration theorem**) holds:
130+
123131
$$|\text{Classes}| = \frac{1}{|G|} \sum_{\pi \in G} k^{C(\pi)}$$
132+
124133
$k$ is the number of values that each representation element can take, in the case of the coloring of a binary tree this would be $k = 2$.
125134

126135
### Evidence
@@ -134,6 +143,7 @@ During the application of $\pi$, the elements in $f$ move via the cycles in the
134143
Since the result should obtain $f \equiv f \pi$, the elements touched by one cycle must all be equal.
135144
At the same time different cycles are independent.
136145
Thus for each permutation cycle $\pi$ we can choose one value (among $k$ possible) and thus we get the number of fixed points:
146+
137147
$$I(\pi) = k^{C(\pi)}$$
138148

139149
## Application: Coloring necklaces
@@ -143,34 +153,39 @@ The task is to count the number of different necklaces from $n$ beads, each of w
143153
When comparing two necklaces, they can be rotated, but not reversed (i.e. a cyclic shift is permitted).
144154

145155
In this problem we can immediately find the group of invariant permutations:
156+
146157
$$\begin{align}
147-
\pi_0 &= 1 2 3 \dots n\\\\
148-
\pi_1 &= 2 3 \dots n 1\\\\
149-
\pi_2 &= 3 \dots n 12\\\\
150-
&\dots\\\\
158+
\pi_0 &= 1 2 3 \dots n\\
159+
\pi_1 &= 2 3 \dots n 1\\
160+
\pi_2 &= 3 \dots n 12\\
161+
&\dots\\
151162
\pi_{n-1} &= n 1 2 3\dots\end{align}$$
152163

153164
Let us find an explicit formula for calculating $C(\pi_i)$.
154165
First we note, that the permutation $\pi_i$ has at the $j$-th position the value $i + j$ (taken modulo $n$).
155166
If we check the cycle structure for $\pi_i$.
156167
We see that $1$ goes to $1 + i$, $1 + i$ goes to $1 + 2i$, which goes to $1 + 3i$, etc., until we come to a number of the form $1 + k n$.
157168
Similar statements can be mode for the remaining elements.
158-
Hence we see that all cycles have the same length, namely $\frac{\\text{lcm}(i, n)}{i} = \frac{n}{\gcd(i, n)}$.
169+
Hence we see that all cycles have the same length, namely $\frac{\text{lcm}(i, n)}{i} = \frac{n}{\gcd(i, n)}$.
159170
Thus the number of cycles in $\pi_i$ will be equal to $\gcd(i, n)$.
160171

161172
Substituting these values into the Pólya enumeration theorem, we obtain the solution:
173+
162174
$$\frac{1}{n} \sum_{i=1}^n k^{\gcd(i, n)}$$
163175

164176
You can leave this formula in this form, or you can simplify it even more.
165177
Let transfer the sum so that it iterates over all divisors of $n$.
166178
In the original sum there will be many equivalent terms: if $i$ is not a divisor of $n$, then such a divisor can be found after computing $\gcd(i, n)$.
167179
Therefore for each divisor $d ~|~ n$ its term $k^{\gcd(d, n)} = k^d$ will appear in the sum multiple times, i.e. the answer to the problem can be rewritten as
180+
168181
$$\frac{1}{n} \sum_{d ~|~ n} C_d k^d,$$
182+
169183
where $C_d$ is the number of such numbers $i$ with $\gcd(i, n) = d$.
170184
We can find an explicit expression for this value.
171185
Any such number $i$ has the form $i = d j$ with $\gcd(j, n / d) = 1$ (otherwise $\gcd(i, n) > d$).
172186
So we can count the number of $j$ with this behavior.
173187
[Euler's phi function](../algebra/phi-function.md) gives us the result $C_d = \phi(n / d)$, and therefore we get the answer:
188+
174189
$$\frac{1}{n} \sum_{d ~|~ n} \phi\left(\frac{n}{d}\right) k^d$$
175190

176191
## Application: Coloring a torus
@@ -198,7 +213,7 @@ It is obvious that all such permutations have the form $p_1^{i_1} p_2^{i_2} p_3^
198213

199214
Thus we can write the implementations to this problem.
200215

201-
```cpp burnside_tori
216+
```{.cpp file=burnside_tori}
202217
using Permutation = vector<int>;
203218

204219
void operator*=(Permutation& p, Permutation const& q) {

src/combinatorics/catalan-numbers.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ The Catalan number $C_n$ is the solution for
3333
There are two formulas for the Catalan numbers: **Recursive and Analytical**. Since, we believe that all the mentioned above problems are equivalent (have the same solution), for the proof of the formulas below we will choose the task which it is easiest to do.
3434

3535
### Recursive formula
36-
$$C_0 = C_1 = 1$$
37-
$$C_n = \sum_{k = 0}^{n-1} C_k C_{n-1-k} , {n} \geq 2$$
36+
37+
$$C_0 = C_1 = 1$$
38+
39+
$$C_n = \sum_{k = 0}^{n-1} C_k C_{n-1-k} , {n} \geq 2$$
3840

3941
The recurrence formula can be easily deduced from the problem of the correct bracket sequence.
4042

@@ -65,6 +67,7 @@ void init() {
6567
```
6668

6769
### Analytical formula
70+
6871
$$C_n = \frac{1}{n + 1} {\binom{2n}{n}}$$
6972

7073
(here $\binom{n}{k}$ denotes the usual binomial coefficient, i.e. number of ways to select $k$ objects from set of $n$ objects).

src/combinatorics/counting_labeled_graphs.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ For each edge $(i, j)$ we can assume that $i < j$ (because the graph is undirect
1616
Therefore the set of all edges has the cardinality $\binom{n}{2}$, i.e. $\frac{n(n-1)}{2}$.
1717

1818
Since any labeled graph is uniquely determined by its edges, the number of labeled graphs with $n$ vertices is equal to:
19+
1920
$$G_n = 2^{\frac{n(n-1)}{2}}$$
2021

2122
## Connected labeled graphs
@@ -32,11 +33,14 @@ Obviously we have $n$ possibilities to root a graph with $n$ labeled vertices, t
3233
The root vertex will appear in a connected component of size $1, \dots n-1$.
3334
There are $k \binom{n}{k} C_k G_{n-k}$ graphs such that the root vertex is in a connected component with $k$ vertices (there are $\binom{n}{k}$ ways to choose $k$ vertices for the component, these are connected in one of $C_k$ ways, the root vertex can be any of the $k$ vertices, and the remainder $n-k$ vertices can be connected/disconnected in any way, which gives a factor of $G_{n-k}$).
3435
Therefore the number of disconnected graphs with $n$ vertices is:
36+
3537
$$\frac{1}{n} \sum_{k=1}^{n-1} k \binom{n}{k} C_k G_{n-k}$$
38+
3639
And finally the number of connected graphs is:
40+
3741
$$C_n = G_n - \frac{1}{n} \sum_{k=1}^{n-1} k \binom{n}{k} C_k G_{n-k}$$
3842

39-
## Labeled graphs with $k$ connected components
43+
## Labeled graphs with $k$ connected components {data-toc-label="Labeled graphs with k connected components"}
4044

4145
Based on the formula from the previous section, we will learn how to count the number of labeled graphs with $n$ vertices and $k$ connected components.
4246

@@ -48,4 +52,5 @@ We use a common approach, we take the last vertex (index $n$).
4852
This vertex belongs to some component.
4953
If the size of this component be $s$, then there are $\binom{n-1}{s-1}$ ways to choose such a set of vertices, and $C_s$ ways to connect them.After removing this component from the graph we have $n-s$ remaining vertices with $k-1$ connected components.
5054
Therefore we obtain the following recurrence relation:
55+
5156
$$D[n][k] = \sum_{s=1}^{n} \binom{n-1}{s-1} C_s D[n-s][k-1]$$

0 commit comments

Comments
 (0)