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
The goal of this project is to translate the wonderful resource
@@ -16,16 +16,21 @@ Compiled pages are published at [https://cp-algorithms.com/](https://cp-algorith
16
16
17
17
## Changelog
18
18
19
+
- July 16, 2024: Major overhaul of the [Finding strongly connected components / Building condensation graph](https://cp-algorithms.com/graph/strongly-connected-components.html) article.
19
20
- June 26, 2023: Added automatic RSS feeds for [new articles](https://cp-algorithms.com/feed_rss_created.xml) and [updates in articles](https://cp-algorithms.com/feed_rss_updated.xml).
20
21
- December 20, 2022: The repository name and the owning organizations were renamed! Now the repo is located at [https://github.com/cp-algorithms/cp-algorithms](https://github.com/cp-algorithms/cp-algorithms). It is recommended to update the upstream link in your local repositories, if you have any.
21
22
- October 31, 2022: It is now possible to select and copy $\LaTeX$ source code of formulas within the articles.
22
23
- June 8, 2022: Tags are enabled. Each article is now marked whether it is translated or original, overall tag info is present in the [tag index](https://cp-algorithms.com/tags.html). For translated articles, clicking on `From: X` tag would lead to the original article.
23
24
- June 7, 2022: Date of last commit and author list with contribution percentage is tracked for each page.
24
-
- June 5, 2022: Enabled content tabs and sidebar navigation. The navigation is moved to a [separate page](https://cp-algorithms.com/navigation.html) and its structure should be adjusted in [navigation.md](https://github.com/cp-algorithms/cp-algorithms/blob/master/src/navigation.md) whenever a new article is created or an old one is moved.
25
+
- June 5, 2022: Enabled content tabs and sidebar navigation. The navigation is moved to a [separate page](https://cp-algorithms.com/navigation.html) and its structure should be adjusted in [navigation.md](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/navigation.md) whenever a new article is created or an old one is moved.
25
26
- January 16, 2022: Switched to the [MkDocs](https://www.mkdocs.org/) site generator with the [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/) theme, which give the website a more modern look, brings a couple of new features (dark mode, better search, ...), makes the website more stable (in terms of rendering math formulas), and makes it easier to contribute.
26
27
27
28
### New articles
28
29
30
+
- (12 July 2024) [Manhattan distance](https://cp-algorithms.com/geometry/manhattan-distance.html)
31
+
- (8 June 2024) [Knapsack Problem](https://cp-algorithms.com/dynamic_programming/knapsack.html)
32
+
- (28 January 2024) [Introduction to Dynamic Programming](https://cp-algorithms.com/dynamic_programming/intro-to-dp.html)
33
+
- (8 December 2023) [Hungarian Algorithm](https://cp-algorithms.com/graph/hungarian-algorithm.html)
29
34
- (10 September 2023) [Tortoise and Hare Algorithm](https://cp-algorithms.com/others/tortoise_and_hare.html)
30
35
- (12 July 2023) [Finding faces of a planar graph](https://cp-algorithms.com/geometry/planar.html)
31
36
- (18 April 2023) [Bit manipulation](https://cp-algorithms.com/algebra/bit-manipulation.html)
@@ -35,7 +40,7 @@ Compiled pages are published at [https://cp-algorithms.com/](https://cp-algorith
35
40
- (7 May 2022) [Knuth's Optimization](https://cp-algorithms.com/dynamic_programming/knuth-optimization.html)
36
41
- (31 March 2022) [Continued fractions](https://cp-algorithms.com/algebra/continued-fractions.html)
37
42
38
-
Full list of updates: [Commit History](https://github.com/cp-algorithms/cp-algorithms/commits/master)
43
+
Full list of updates: [Commit History](https://github.com/cp-algorithms/cp-algorithms/commits/main)
39
44
40
45
Full list of articles: [Navigation](https://cp-algorithms.com/navigation.html)
Copy file name to clipboardExpand all lines: src/algebra/big-integer.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
@@ -166,7 +166,7 @@ This method is often used for calculations modulo non-prime number M; in this ca
166
166
167
167
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.
168
168
169
-
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.
169
+
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](garners-algorithm.md) allows to restore the number from such representation to normal integer.
170
170
171
171
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).
Copy file name to clipboardExpand all lines: src/algebra/bit-manipulation.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -152,7 +152,7 @@ bool isPowerOfTwo(unsigned int n) {
152
152
}
153
153
```
154
154
155
-
### Clear the most-right set bit
155
+
### Clear the right-most set bit
156
156
157
157
The expression $n ~\&~ (n-1)$ can be used to turn off the rightmost set bit of a number $n$.
158
158
This works because the expression $n-1$ flips all bits after the rightmost set bit of $n$, including the rightmost set bit.
@@ -186,6 +186,44 @@ int countSetBits(int n)
186
186
}
187
187
```
188
188
189
+
### Count set bits upto $n$
190
+
To count the number of set bits of all numbers upto the number $n$ (inclusive), we can run the Brian Kernighan's algorithm on all numbers upto $n$. But this will result in a "Time Limit Exceeded" in contest submissions.
191
+
192
+
We can use the fact that for numbers upto $2^x$ (i.e. from $1$ to $2^x - 1$) there are $x \cdot 2^{x-1}$ set bits. This can be visualised as follows.
193
+
```
194
+
0 -> 0 0 0 0
195
+
1 -> 0 0 0 1
196
+
2 -> 0 0 1 0
197
+
3 -> 0 0 1 1
198
+
4 -> 0 1 0 0
199
+
5 -> 0 1 0 1
200
+
6 -> 0 1 1 0
201
+
7 -> 0 1 1 1
202
+
8 -> 1 0 0 0
203
+
```
204
+
205
+
We can see that the all the columns except the leftmost have $4$ (i.e. $2^2$) set bits each, i.e. upto the number $2^3 - 1$, the number of set bits is $3 \cdot 2^{3-1}$.
206
+
207
+
With the new knowledge in hand we can come up with the following algorithm:
208
+
209
+
- Find the highest power of $2$ that is lesser than or equal to the given number. Let this number be $x$.
210
+
- Calculate the number of set bits from $1$ to $2^x - 1$ by using the formua $x \cdot 2^{x-1}$.
211
+
- Count the no. of set bits in the most significant bit from $2^x$ to $n$ and add it.
212
+
- Subtract $2^x$ from $n$ and repeat the above steps using the new $n$.
0 commit comments