Skip to content

Commit 5000679

Browse files
committed
Fix language annotation of code blocks
1 parent 7f72ab3 commit 5000679

7 files changed

Lines changed: 50 additions & 44 deletions

File tree

src/algebra/big-integer.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ Here we describe long arithmetic for only non-negative integers. To extend the a
1616

1717
We'll store numbers as a `vector<int>`, in which each element is a single "digit" of the number.
1818

19-
```
19+
```cpp
2020
typedef vector<int> lnum;
2121
```
2222

2323
To improve performance we'll use $10^9$ as the base, so that each "digit" of the long number contains 9 decimal digits at once.
2424

25-
```
25+
```cpp
2626
const int base = 1000*1000*1000;
2727
```
2828

@@ -32,7 +32,7 @@ Digits will be stored in order from least to most significant. All operations wi
3232

3333
Printing the long integer is the easiest operation. First we print the last element of the vector (or 0 if the vector is empty), followed by the rest of the elements padded with leading zeros if necessary so that they are exactly 9 digits long.
3434

35-
```
35+
```cpp
3636
printf ("%d", a.empty() ? 0 : a.back());
3737
for (int i=(int)a.size()-2; i>=0; --i)
3838
printf ("%09d", a[i]);
@@ -44,7 +44,7 @@ Note that we cast `a.size()` to integer to avoid unsigned integer underflow if v
4444
4545
To read a long integer, read its notation into a `string` and then convert it to "digits":
4646
47-
```
47+
```cpp
4848
for (int i=(int)s.length(); i>0; i-=9)
4949
if (i < 9)
5050
a.push_back (atoi (s.substr (0, i).c_str()));
@@ -54,7 +54,7 @@ for (int i=(int)s.length(); i>0; i-=9)
5454

5555
If we use an array of `char` instead of a `string`, the code will be even shorter:
5656

57-
```
57+
```cpp
5858
for (int i=(int)strlen(s); i>0; i-=9) {
5959
s[i] = 0;
6060
a.push_back (atoi (i>=9 ? s+i-9 : s));
@@ -63,7 +63,7 @@ for (int i=(int)strlen(s); i>0; i-=9) {
6363

6464
If the input can contain leading zeros, they can be removed as follows:
6565

66-
```
66+
```cpp
6767
while (a.size() > 1 && a.back() == 0)
6868
a.pop_back();
6969
```
@@ -72,7 +72,7 @@ while (a.size() > 1 && a.back() == 0)
7272

7373
Increment long integer $a$ by $b$ and store result in $a$:
7474

75-
```
75+
```cpp
7676
int carry = 0;
7777
for (size_t i=0; i<max(a.size(),b.size()) || carry; ++i) {
7878
if (i == a.size())
@@ -87,7 +87,7 @@ for (size_t i=0; i<max(a.size(),b.size()) || carry; ++i) {
8787

8888
Decrement long integer $a$ by $b$ ($a \ge b$) and store result in $a$:
8989

90-
```
90+
```cpp
9191
int carry = 0;
9292
for (size_t i=0; i<b.size() || carry; ++i) {
9393
a[i] -= carry + (i < b.size() ? b[i] : 0);
@@ -104,7 +104,7 @@ Note that after performing subtraction we remove leading zeros to keep up with t
104104

105105
Multiply long integer $a$ by short integer $b$ ($b < base$) and store result in $a$:
106106

107-
```
107+
```cpp
108108
int carry = 0;
109109
for (size_t i=0; i<a.size() || carry; ++i) {
110110
if (i == a.size())
@@ -123,7 +123,7 @@ Additional optimization: If runtime is extremely important, you can try to repla
123123

124124
Multiply long integers $a$ and $b$ and store result in $c$:
125125

126-
```
126+
```cpp
127127
lnum c (a.size()+b.size());
128128
for (size_t i=0; i<a.size(); ++i)
129129
for (int j=0, carry=0; j<(int)b.size() || carry; ++j) {
@@ -139,7 +139,7 @@ while (c.size() > 1 && c.back() == 0)
139139
140140
Divide long integer $a$ by short integer $b$ ($b < base$), store integer result in $a$ and remainder in `carry`:
141141
142-
```
142+
```cpp
143143
int carry = 0;
144144
for (int i=(int)a.size()-1; i>=0; --i) {
145145
long long cur = a[i] + carry * 1ll * base;

src/algebra/gray-code.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This code was invented by Frank Gray in 1953.
1212

1313
Let's look at the bits of number $n$ and the bits of number $G(n)$. Notice that $i$-th bit of $G(n)$ equals 1 only when $i$-th bit of $n$ equals 1 and $i + 1$-th bit equals 0 or the other way around ($i$-th bit equals 0 and $i + 1$-th bit equals 1). Thus, $G(n) = n \oplus (n >> 1)$:
1414

15-
```
15+
```cpp
1616
int g (int n) {
1717
return n ^ (n >> 1);
1818
}
@@ -32,7 +32,7 @@ We will move from the most significant bits to the least significant ones (the l
3232
3333
The easiest way to write it in code is:
3434
35-
```
35+
```cpp
3636
int rev_g (int g) {
3737
int n = 0;
3838
for (; g; g >>= 1)

src/algebra/phi-function.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,37 @@ $$= n \cdot \left(1 - \frac{1}{p_1}\right) \cdot \left(1 - \frac{1}{p_2}\right)
3232

3333
`C++` implementation using factorization in $O(\sqrt{n})$ <span class="toggle-code">Show/Hide</span>
3434

35-
int phi(int n) {
36-
int result = n;
37-
for(int i = 2; i * i <= n; ++i)
38-
if(n % i == 0) {
39-
while(n % i == 0)
40-
n /= i;
41-
result -= result / i;
42-
}
43-
if(n > 1)
44-
result -= result / n;
45-
return result;
46-
}
35+
```cpp
36+
int phi(int n) {
37+
int result = n;
38+
for(int i = 2; i * i <= n; ++i)
39+
if(n % i == 0) {
40+
while(n % i == 0)
41+
n /= i;
42+
result -= result / i;
43+
}
44+
if(n > 1)
45+
result -= result / n;
46+
return result;
47+
}
48+
```
4749
4850
`Python 3` implementation <span class="toggle-code">Show/Hide</span>
4951
50-
def phi(n):
51-
result = n
52-
i = 2
53-
while i * i <= n:
54-
if n % i == 0:
55-
while n % i == 0:
56-
n //= i
57-
result -= result // i
58-
i += 1
59-
if n > 1:
60-
result -= result // n
61-
return result
52+
```python
53+
def phi(n):
54+
result = n
55+
i = 2
56+
while i * i <= n:
57+
if n % i == 0:
58+
while n % i == 0:
59+
n //= i
60+
result -= result // i
61+
i += 1
62+
if n > 1:
63+
result -= result // n
64+
return result
65+
```
6266

6367
## Applications of Euler's totient function
6468

src/data_structures/sqrt-tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Note that when we do the recursive call, we do prefix or suffix $\text{massUpdat
186186

187187
The following implementation of Sqrt Tree can perform the following operations: build in $O(n \cdot \log \log n)$, answer queries in $O(1)$ and update an element in $O(\sqrt{n})$.
188188

189-
~~~~~
189+
~~~~~cpp
190190
SqrtTreeItem op(const SqrtTreeItem &a, const SqrtTreeItem &b);
191191

192192
inline int log2Up(int n) {

src/data_structures/treap.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ We implement **Build** operation with $O (N \log N)$ complexity using $N$ **Inse
5353

5454
## Implementation
5555

56-
```
56+
```cpp
5757
struct item {
5858
int key, prior;
5959
item * l, * r;
@@ -113,7 +113,7 @@ To extend the functionality of the treap, it is often necessary to store the num
113113
114114
When a tree changes (nodes are added or removed etc.), `cnt` of some nodes should be updated accordingly. We'll create two functions: `cnt()` will return the current value of `cnt` or 0 if the node does not exist, and `upd_cnt()` will update the value of `cnt` for this node assuming that for its children L and R the values of `cnt` have already been updated. Evidently it's sufficient to add calls of `upd_cnt()` to the end of `insert`, `erase`, `split` and `merge` to keep `cnt` values up-to-date.
115115
116-
```
116+
```cpp
117117
int cnt (pitem t) {
118118
return t ? t->cnt : 0;
119119
}
@@ -147,7 +147,7 @@ Now it's clear how to calculate the implicit key of current node quickly. Since
147147

148148
Here are the new implementations of **Split** and **Merge**:
149149

150-
```
150+
```cpp
151151
void merge (pitem & t, pitem l, pitem r) {
152152
if (!l || !r)
153153
t = l ? l : r;
@@ -187,7 +187,7 @@ Now let's consider the implementation of various operations on implicit treaps:
187187
188188
Here is an example implementation of the implicit treap with reverse on the interval. For each node we store field called `value` which is the actual value of the array element at current position. We also provide implementation of the function `output()`, which outputs an array that corresponds to the current state of the implicit treap.
189189
190-
```
190+
```cpp
191191
typedef struct item * pitem;
192192
struct item {
193193
int prior, value, cnt;

src/geometry/area-of-simple-polygon.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $$A = \sum_{(p,q)\in \text{edges}} \frac{(p_x - q_x) \cdot (p_y + q_y)}{2}$$
1212

1313
Code:
1414

15-
```
15+
```cpp
1616
double area(const vector<point>& fig) {
1717
double res = 0;
1818
for (unsigned i = 0; i < fig.size(); i++) {
@@ -27,4 +27,4 @@ double area(const vector<point>& fig) {
2727
## Method 2
2828
We can choose a point $O$ arbitrarily, iterate over all edges adding the oriented area of the triangle formed by the edge and point $O$. Again, due to the sign of area, extra area will be reduced.
2929
30-
This method is better as it can be generalized to more complex cases (such as when some sides are arcs instead of straight lines)
30+
This method is better as it can be generalized to more complex cases (such as when some sides are arcs instead of straight lines)

src/graph/strongly-connected-components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Algorithm asymptotics is $O(n + m)$, because it is just two depth (breadth) firs
4949
Finally, it is appropriate to mention [topological sort](http://e-maxx-eng.appspot.com/graph/topological-sort.html) here. First of all, step 1 of the algorithm represents topological sort of graph $G$ (actually this is exactly what vertices' sort by exit time means). Secondly, the algorithm's scheme generates strongly connected components by decreasing order of their exit times, thus it generates components - vertices of condensation graph - in topological sort order.
5050

5151
## Implementation
52+
```cpp
5253
vector < vector<int> > g, gr;
5354
vector<char> used;
5455
vector<int> order, component;
@@ -93,6 +94,7 @@ Finally, it is appropriate to mention [topological sort](http://e-maxx-eng.appsp
9394
}
9495
}
9596
}
97+
```
9698
9799
Here, $g$ is graph, $gr$ is transposed graph. Function $dfs1$ implements depth first search on graph $G$, function $dfs2$ - on transposed graph $G^T$. Function $dfs1$ fills the list $order$ with vertices in increasing order of their exit times (actually, it is making a topological sort). Function $dfs2$ stores all reached vertices in list $component$, that is going to store next strongly connected component after each run.
98100

0 commit comments

Comments
 (0)