problem
stringlengths
1.19k
65.4k
solution
stringlengths
1.19k
67.5k
topic
stringlengths
5
80
--- title prime_sieve_linear --- # Linear Sieve Given a number $n$, find all prime numbers in a segment $[2;n]$. The standard way of solving a task is to use [the sieve of Eratosthenes](sieve-of-eratosthenes.md). This algorithm is very simple, but it has runtime $O(n \log \log n)$. Although there are a lot of know...
--- title prime_sieve_linear --- # Linear Sieve Given a number $n$, find all prime numbers in a segment $[2;n]$. The standard way of solving a task is to use [the sieve of Eratosthenes](sieve-of-eratosthenes.md). This algorithm is very simple, but it has runtime $O(n \log \log n)$. Although there are a lot of know...
Linear Sieve
--- title big_integer --- # Arbitrary-Precision Arithmetic Arbitrary-Precision arithmetic, also known as "bignum" or simply "long arithmetic" is a set of data structures and algorithms which allows to process much greater numbers than can be fit in standard data types. Here are several types of arbitrary-precision a...
--- title big_integer --- # Arbitrary-Precision Arithmetic Arbitrary-Precision arithmetic, also known as "bignum" or simply "long arithmetic" is a set of data structures and algorithms which allows to process much greater numbers than can be fit in standard data types. Here are several types of arbitrary-precision a...
Arbitrary-Precision Arithmetic
--- title gray_code --- # Gray code Gray code is a binary numeral system where two successive values differ in only one bit. For example, the sequence of Gray codes for 3-bit numbers is: 000, 001, 011, 010, 110, 111, 101, 100, so $G(4) = 6$. This code was invented by Frank Gray in 1953. ## Finding Gray code Let...
--- title gray_code --- # Gray code Gray code is a binary numeral system where two successive values differ in only one bit. For example, the sequence of Gray codes for 3-bit numbers is: 000, 001, 011, 010, 110, 111, 101, 100, so $G(4) = 6$. This code was invented by Frank Gray in 1953. ## Finding Gray code Let...
Gray code
--- title diofant_2_equation --- # Linear Diophantine Equation A Linear Diophantine Equation (in two variables) is an equation of the general form: $$ax + by = c$$ where $a$, $b$, $c$ are given integers, and $x$, $y$ are unknown integers. In this article, we consider several classical problems on these equations:...
--- title diofant_2_equation --- # Linear Diophantine Equation A Linear Diophantine Equation (in two variables) is an equation of the general form: $$ax + by = c$$ where $a$, $b$, $c$ are given integers, and $x$, $y$ are unknown integers. In this article, we consider several classical problems on these equations:...
Linear Diophantine Equation
--- title discrete_log --- # Discrete Logarithm The discrete logarithm is an integer $x$ satisfying the equation $$a^x \equiv b \pmod m$$ for given integers $a$, $b$ and $m$. The discrete logarithm does not always exist, for instance there is no solution to $2^x \equiv 3 \pmod 7$. There is no simple condition to ...
--- title discrete_log --- # Discrete Logarithm The discrete logarithm is an integer $x$ satisfying the equation $$a^x \equiv b \pmod m$$ for given integers $a$, $b$ and $m$. The discrete logarithm does not always exist, for instance there is no solution to $2^x \equiv 3 \pmod 7$. There is no simple condition to ...
Discrete Logarithm
--- title: Factorial modulo p title modular_factorial --- # Factorial modulo $p$ In some cases it is necessary to consider complex formulas modulo some prime $p$, containing factorials in both numerator and denominator, like such that you encounter in the formula for Binomial coefficients. We consider the case when ...
--- title: Factorial modulo p title modular_factorial --- # Factorial modulo $p$ In some cases it is necessary to consider complex formulas modulo some prime $p$, containing factorials in both numerator and denominator, like such that you encounter in the formula for Binomial coefficients. We consider the case when ...
Factorial modulo $p$
--- title primitive_root --- # Primitive Root ## Definition In modular arithmetic, a number $g$ is called a `primitive root modulo n` if every number coprime to $n$ is congruent to a power of $g$ modulo $n$. Mathematically, $g$ is a `primitive root modulo n` if and only if for any integer $a$ such that $\gcd(a, n) ...
--- title primitive_root --- # Primitive Root ## Definition In modular arithmetic, a number $g$ is called a `primitive root modulo n` if every number coprime to $n$ is congruent to a power of $g$ modulo $n$. Mathematically, $g$ is a `primitive root modulo n` if and only if for any integer $a$ such that $\gcd(a, n) ...
Primitive Root
--- title - Original --- # Binary Exponentiation by Factoring Consider a problem of computing $ax^y \pmod{2^d}$, given integers $a$, $x$, $y$ and $d \geq 3$, where $x$ is odd. The algorithm below allows to solve this problem with $O(d)$ additions and binary operations and a single multiplication by $y$. Due to th...
--- title - Original --- # Binary Exponentiation by Factoring Consider a problem of computing $ax^y \pmod{2^d}$, given integers $a$, $x$, $y$ and $d \geq 3$, where $x$ is odd. The algorithm below allows to solve this problem with $O(d)$ additions and binary operations and a single multiplication by $y$. Due to th...
Binary Exponentiation by Factoring
--- title reverse_element --- # Modular Multiplicative Inverse ## Definition A [modular multiplicative inverse](http://en.wikipedia.org/wiki/Modular_multiplicative_inverse) of an integer $a$ is an integer $x$ such that $a \cdot x$ is congruent to $1$ modular some modulus $m$. To write it in a formal way: we want to...
--- title reverse_element --- # Modular Multiplicative Inverse ## Definition A [modular multiplicative inverse](http://en.wikipedia.org/wiki/Modular_multiplicative_inverse) of an integer $a$ is an integer $x$ such that $a \cdot x$ is congruent to $1$ modular some modulus $m$. To write it in a formal way: we want to...
Modular Multiplicative Inverse
--- title - Original --- # Bit manipulation ## Binary number A **binary number** is a number expressed in the base-2 numeral system or binary numeral system, it is a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one). We say that a certain bit is **set**, if it is one...
--- title - Original --- # Bit manipulation ## Binary number A **binary number** is a number expressed in the base-2 numeral system or binary numeral system, it is a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one). We say that a certain bit is **set**, if it is one...
Bit manipulation
--- title fibonacci_numbers --- # Fibonacci Numbers The Fibonacci sequence is defined as follows: $$F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}$$ The first elements of the sequence ([OEIS A000045](http://oeis.org/A000045)) are: $$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...$$ ## Properties Fibonacci numbers posse...
--- title fibonacci_numbers --- # Fibonacci Numbers The Fibonacci sequence is defined as follows: $$F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}$$ The first elements of the sequence ([OEIS A000045](http://oeis.org/A000045)) are: $$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...$$ ## Properties Fibonacci numbers posse...
Fibonacci Numbers
--- title euler_function --- # Euler's totient function Euler's totient function, also known as **phi-function** $\phi (n)$, counts the number of integers between 1 and $n$ inclusive, which are coprime to $n$. Two numbers are coprime if their greatest common divisor equals $1$ ($1$ is considered to be coprime to any...
--- title euler_function --- # Euler's totient function Euler's totient function, also known as **phi-function** $\phi (n)$, counts the number of integers between 1 and $n$ inclusive, which are coprime to $n$. Two numbers are coprime if their greatest common divisor equals $1$ ($1$ is considered to be coprime to any...
Euler's totient function
--- title - Original --- # Number of divisors / sum of divisors In this article we discuss how to compute the number of divisors $d(n)$ and the sum of divisors $\sigma(n)$ of a given number $n$. ## Number of divisors It should be obvious that the prime factorization of a divisor $d$ has to be a subset of the prim...
--- title - Original --- # Number of divisors / sum of divisors In this article we discuss how to compute the number of divisors $d(n)$ and the sum of divisors $\sigma(n)$ of a given number $n$. ## Number of divisors It should be obvious that the prime factorization of a divisor $d$ has to be a subset of the prim...
Number of divisors / sum of divisors
--- title chinese_theorem --- # Chinese Remainder Theorem The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi. ## Formulation Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In addition to $m_i$, we are al...
--- title chinese_theorem --- # Chinese Remainder Theorem The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi. ## Formulation Let $m = m_1 \cdot m_2 \cdots m_k$, where $m_i$ are pairwise coprime. In addition to $m_i$, we are al...
Chinese Remainder Theorem
--- title extended_euclid_algorithm --- # Extended Euclidean Algorithm 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 ...
--- title extended_euclid_algorithm --- # Extended Euclidean Algorithm 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 ...
Extended Euclidean Algorithm
--- title fft_multiply --- # Fast Fourier transform 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 also multiplying two long numbers can be reduced to mul...
--- title fft_multiply --- # Fast Fourier transform 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 also multiplying two long numbers can be reduced to mul...
Fast Fourier transform
--- title diofant_1_equation --- # Linear Congruence Equation This equation is of the form: $$a \cdot x \equiv b \pmod n,$$ where $a$, $b$ and $n$ are given integers and $x$ is an unknown integer. It is required to find the value $x$ from the interval $[0, n-1]$ (clearly, on the entire number line there can be in...
--- title diofant_1_equation --- # Linear Congruence Equation This equation is of the form: $$a \cdot x \equiv b \pmod n,$$ where $a$, $b$ and $n$ are given integers and $x$ is an unknown integer. It is required to find the value $x$ from the interval $[0, n-1]$ (clearly, on the entire number line there can be in...
Linear Congruence Equation
--- title - Original --- # Montgomery Multiplication Many algorithms in number theory, like [prime testing](primality_tests.md) or [integer factorization](factorization.md), and in cryptography, like RSA, require lots of operations modulo a large number. A multiplications like $x y \bmod{n}$ is quite slow to comput...
--- title - Original --- # Montgomery Multiplication Many algorithms in number theory, like [prime testing](primality_tests.md) or [integer factorization](factorization.md), and in cryptography, like RSA, require lots of operations modulo a large number. A multiplications like $x y \bmod{n}$ is quite slow to comput...
Montgomery Multiplication
--- title - Original --- # Primality tests This article describes multiple algorithms to determine if a number is prime or not. ## Trial division By definition a prime number doesn't have any divisors other than $1$ and itself. A composite number has at least one additional divisor, let's call it $d$. Naturally...
--- title - Original --- # Primality tests This article describes multiple algorithms to determine if a number is prime or not. ## Trial division By definition a prime number doesn't have any divisors other than $1$ and itself. A composite number has at least one additional divisor, let's call it $d$. Naturally...
Primality tests
--- title euclid_algorithm --- # Euclidean algorithm for computing the greatest common divisor 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$. It's commonly denoted by $\gcd(a, b)$. Mathematically it...
--- title euclid_algorithm --- # Euclidean algorithm for computing the greatest common divisor 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$. It's commonly denoted by $\gcd(a, b)$. Mathematically it...
Euclidean algorithm for computing the greatest common divisor
--- title: K-th order statistic in O(N) title kth_order_statistics --- # $K$th order statistic in $O(N)$ Given an array $A$ of size $N$ and a number $K$. The problem is to find $K$-th largest number in the array, i.e., $K$-th order statistic. The basic idea - to use the idea of quick sort algorithm. Actually, the al...
--- title: K-th order statistic in O(N) title kth_order_statistics --- # $K$th order statistic in $O(N)$ Given an array $A$ of size $N$ and a number $K$. The problem is to find $K$-th largest number in the array, i.e., $K$-th order statistic. The basic idea - to use the idea of quick sort algorithm. Actually, the al...
$K$th order statistic in $O(N)$
--- title longest_increasing_subseq_log --- # Longest increasing subsequence We are given an array with $n$ numbers: $a[0 \dots n-1]$. The task is to find the longest, strictly increasing, subsequence in $a$. Formally we look for the longest sequence of indices $i_1, \dots i_k$ such that $$i_1 < i_2 < \dots < i_k,...
--- title longest_increasing_subseq_log --- # Longest increasing subsequence We are given an array with $n$ numbers: $a[0 \dots n-1]$. The task is to find the longest, strictly increasing, subsequence in $a$. Formally we look for the longest sequence of indices $i_1, \dots i_k$ such that $$i_1 < i_2 < \dots < i_k,...
Longest increasing subsequence
--- title - Original title: MEX (minimal excluded) of a sequence --- # MEX (minimal excluded) of a sequence Given an array $A$ of size $N$. You have to find the minimal non-negative element that is not present in the array. That number is commonly called the **MEX** (minimal excluded). $$ \begin{align} \text{mex}...
--- title - Original title: MEX (minimal excluded) of a sequence --- # MEX (minimal excluded) of a sequence Given an array $A$ of size $N$. You have to find the minimal non-negative element that is not present in the array. That number is commonly called the **MEX** (minimal excluded). $$ \begin{align} \text{mex}...
MEX (minimal excluded) of a sequence
--- title rmq --- # Range Minimum Query You are given an array $A[1..N]$. You have to answer incoming queries of the form $(L, R)$, which ask to find the minimum element in array $A$ between positions $L$ and $R$ inclusive. RMQ can appear in problems directly or can be applied in some other tasks, e.g. the [Lowest ...
--- title rmq --- # Range Minimum Query You are given an array $A[1..N]$. You have to answer incoming queries of the form $(L, R)$, which ask to find the minimum element in array $A$ between positions $L$ and $R$ inclusive. RMQ can appear in problems directly or can be applied in some other tasks, e.g. the [Lowest ...
Range Minimum Query
--- title randomized_heap --- # Randomized Heap A randomized heap is a heap that, through using randomization, allows to perform all operations in expected logarithmic time. A **min heap** is a binary tree in which the value of each vertex is less than or equal to the values of its children. Thus the minimum of the...
--- title randomized_heap --- # Randomized Heap A randomized heap is a heap that, through using randomization, allows to perform all operations in expected logarithmic time. A **min heap** is a binary tree in which the value of each vertex is less than or equal to the values of its children. Thus the minimum of the...
Randomized Heap
--- title fenwick_tree --- # Fenwick Tree Let, $f$ be some group operation (binary associative function over a set with identity element and inverse elements) and $A$ be an array of integers of length $N$. Fenwick tree is a data structure which: * calculates the value of function $f$ in the given range $[l, r]$ (i...
--- title fenwick_tree --- # Fenwick Tree Let, $f$ be some group operation (binary associative function over a set with identity element and inverse elements) and $A$ be an array of integers of length $N$. Fenwick tree is a data structure which: * calculates the value of function $f$ in the given range $[l, r]$ (i...
Fenwick Tree
--- title - Original --- # Sparse Table Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in $O(\log n)$, but its true power is answering range minimum queries (or equivalent range maximum queries). For those queries it can compute the answer in $O(1)$ time. Th...
--- title - Original --- # Sparse Table Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in $O(\log n)$, but its true power is answering range minimum queries (or equivalent range maximum queries). For those queries it can compute the answer in $O(1)$ time. Th...
Sparse Table
--- title treap --- # Treap (Cartesian tree) A treap is a data structure which combines binary tree and binary heap (hence the name: tree + heap $\Rightarrow$ Treap). More specifically, treap is a data structure that stores pairs $(X, Y)$ in a binary tree in such a way that it is a binary search tree by $X$ and a b...
--- title treap --- # Treap (Cartesian tree) A treap is a data structure which combines binary tree and binary heap (hence the name: tree + heap $\Rightarrow$ Treap). More specifically, treap is a data structure that stores pairs $(X, Y)$ in a binary tree in such a way that it is a binary search tree by $X$ and a b...
Treap (Cartesian tree)
--- title: Deleting from a data structure in O(T(n) log n) title - Original --- # Deleting from a data structure in $O(T(n)\log n)$ Suppose you have a data structure which allows adding elements in **true** $O(T(n))$. This article will describe a technique that allows deletion in $O(T(n)\log n)$ offline. ## Algorit...
--- title: Deleting from a data structure in O(T(n) log n) title - Original --- # Deleting from a data structure in $O(T(n)\log n)$ Suppose you have a data structure which allows adding elements in **true** $O(T(n))$. This article will describe a technique that allows deletion in $O(T(n)\log n)$ offline. ## Algorit...
Deleting from a data structure in $O(T(n)\log n)$
--- title segment_tree --- # Segment Tree A Segment Tree is a data structure that stores information about array intervals as a tree. This allows answering range queries over an array efficiently, while still being flexible enough to allow quick modification of the array. This includes finding the sum of consecutive...
--- title segment_tree --- # Segment Tree A Segment Tree is a data structure that stores information about array intervals as a tree. This allows answering range queries over an array efficiently, while still being flexible enough to allow quick modification of the array. This includes finding the sum of consecutive...
Segment Tree
--- title - Original --- # Sqrt Tree Given an array $a$ that contains $n$ elements and the operation $\circ$ that satisfies associative property: $(x \circ y) \circ z = x \circ (y \circ z)$ is true for any $x$, $y$, $z$. So, such operations as $\gcd$, $\min$, $\max$, $+$, $\text{and}$, $\text{or}$, $\text{xor}$, e...
--- title - Original --- # Sqrt Tree Given an array $a$ that contains $n$ elements and the operation $\circ$ that satisfies associative property: $(x \circ y) \circ z = x \circ (y \circ z)$ is true for any $x$, $y$, $z$. So, such operations as $\gcd$, $\min$, $\max$, $+$, $\text{and}$, $\text{or}$, $\text{xor}$, e...
Sqrt Tree
--- title sqrt_decomposition --- # Sqrt Decomposition Sqrt Decomposition is a method (or a data structure) that allows you to perform some common operations (finding sum of the elements of the sub-array, finding the minimal/maximal element, etc.) in $O(\sqrt n)$ operations, which is much faster than $O(n)$ for the t...
--- title sqrt_decomposition --- # Sqrt Decomposition Sqrt Decomposition is a method (or a data structure) that allows you to perform some common operations (finding sum of the elements of the sub-array, finding the minimal/maximal element, etc.) in $O(\sqrt n)$ operations, which is much faster than $O(n)$ for the t...
Sqrt Decomposition
--- title stacks_for_minima --- # Minimum stack / Minimum queue In this article we will consider three problems: first we will modify a stack in a way that allows us to find the smallest element of the stack in $O(1)$, then we will do the same thing with a queue, and finally we will use these data structures to fin...
--- title stacks_for_minima --- # Minimum stack / Minimum queue In this article we will consider three problems: first we will modify a stack in a way that allows us to find the smallest element of the stack in $O(1)$, then we will do the same thing with a queue, and finally we will use these data structures to fin...
Minimum stack / Minimum queue
--- title dsu --- # Disjoint Set Union This article discusses the data structure **Disjoint Set Union** or **DSU**. Often it is also called **Union Find** because of its two main operations. This data structure provides the following capabilities. We are given several elements, each of which is a separate set. A DS...
--- title dsu --- # Disjoint Set Union This article discusses the data structure **Disjoint Set Union** or **DSU**. Often it is also called **Union Find** because of its two main operations. This data structure provides the following capabilities. We are given several elements, each of which is a separate set. A DS...
Disjoint Set Union
--- title fenwick_tree --- # Fenwick Tree Let, $f$ be some group operation (binary associative function over a set with identity element and inverse elements) and $A$ be an array of integers of length $N$. Fenwick tree is a data structure which: * calculates the value of function $f$ in the given range $[l, r]$ (i...
--- title fenwick_tree --- # Fenwick Tree Let, $f$ be some group operation (binary associative function over a set with identity element and inverse elements) and $A$ be an array of integers of length $N$. Fenwick tree is a data structure which: * calculates the value of function $f$ in the given range $[l, r]$ (i...
Fenwick Tree
--- title suffix_automata --- # Suffix Automaton A **suffix automaton** is a powerful data structure that allows solving many string-related problems. For example, you can search for all occurrences of one string in another, or count the amount of different substrings of a given string. Both tasks can be solved in...
--- title suffix_automata --- # Suffix Automaton A **suffix automaton** is a powerful data structure that allows solving many string-related problems. For example, you can search for all occurrences of one string in another, or count the amount of different substrings of a given string. Both tasks can be solved in...
Suffix Automaton
--- title expressions_parsing --- # Expression parsing A string containing a mathematical expression containing numbers and various operators is given. We have to compute the value of it in $O(n)$, where $n$ is the length of the string. The algorithm discussed here translates an expression into the so-called **reve...
--- title expressions_parsing --- # Expression parsing A string containing a mathematical expression containing numbers and various operators is given. We have to compute the value of it in $O(n)$, where $n$ is the length of the string. The algorithm discussed here translates an expression into the so-called **reve...
Expression parsing
--- title prefix_function --- # Prefix function. Knuth–Morris–Pratt algorithm ## Prefix function definition You are given a string $s$ of length $n$. The **prefix function** for this string is defined as an array $\pi$ of length $n$, where $\pi[i]$ is the length of the longest proper prefix of the substring $s[0 \d...
--- title prefix_function --- # Prefix function. Knuth–Morris–Pratt algorithm ## Prefix function definition You are given a string $s$ of length $n$. The **prefix function** for this string is defined as an array $\pi$ of length $n$, where $\pi[i]$ is the length of the longest proper prefix of the substring $s[0 \d...
Prefix function. Knuth–Morris–Pratt algorithm
--- title duval_algorithm --- # Lyndon factorization ## Lyndon factorization First let us define the notion of the Lyndon factorization. A string is called **simple** (or a Lyndon word), if it is strictly **smaller than** any of its own nontrivial **suffixes**. Examples of simple strings are: $a$, $b$, $ab$, $aab$...
--- title duval_algorithm --- # Lyndon factorization ## Lyndon factorization First let us define the notion of the Lyndon factorization. A string is called **simple** (or a Lyndon word), if it is strictly **smaller than** any of its own nontrivial **suffixes**. Examples of simple strings are: $a$, $b$, $ab$, $aab$...
Lyndon factorization
--- title string_hashes --- # String Hashing Hashing algorithms are helpful in solving a lot of problems. We want to solve the problem of comparing strings efficiently. The brute force way of doing so is just to compare the letters of both strings, which has a time complexity of $O(\min(n_1, n_2))$ if $n_1$ and $n_...
--- title string_hashes --- # String Hashing Hashing algorithms are helpful in solving a lot of problems. We want to solve the problem of comparing strings efficiently. The brute force way of doing so is just to compare the letters of both strings, which has a time complexity of $O(\min(n_1, n_2))$ if $n_1$ and $n_...
String Hashing
--- title ukkonen --- # Suffix Tree. Ukkonen's Algorithm *This article is a stub and doesn't contain any descriptions. For a description of the algorithm, refer to other sources, such as [Algorithms on Strings, Trees, and Sequences](https://www.cs.cmu.edu/afs/cs/project/pscico-guyb/realworld/www/slidesF06/cmuonly/gu...
--- title ukkonen --- # Suffix Tree. Ukkonen's Algorithm *This article is a stub and doesn't contain any descriptions. For a description of the algorithm, refer to other sources, such as [Algorithms on Strings, Trees, and Sequences](https://www.cs.cmu.edu/afs/cs/project/pscico-guyb/realworld/www/slidesF06/cmuonly/gu...
Suffix Tree. Ukkonen's Algorithm
--- title: Manacher's Algorithm - Finding all sub-palindromes in O(N) title palindromes_count --- # Manacher's Algorithm - Finding all sub-palindromes in $O(N)$ ## Statement Given string $s$ with length $n$. Find all the pairs $(i, j)$ such that substring $s[i\dots j]$ is a palindrome. String $t$ is a palindrome whe...
--- title: Manacher's Algorithm - Finding all sub-palindromes in O(N) title palindromes_count --- # Manacher's Algorithm - Finding all sub-palindromes in $O(N)$ ## Statement Given string $s$ with length $n$. Find all the pairs $(i, j)$ such that substring $s[i\dots j]$ is a palindrome. String $t$ is a palindrome whe...
Manacher's Algorithm - Finding all sub-palindromes in $O(N)$
--- title string_tandems --- # Finding repetitions Given a string $s$ of length $n$. A **repetition** is two occurrences of a string in a row. In other words a repetition can be described by a pair of indices $i < j$ such that the substring $s[i \dots j]$ consists of two identical strings written after each other. ...
--- title string_tandems --- # Finding repetitions Given a string $s$ of length $n$. A **repetition** is two occurrences of a string in a row. In other words a repetition can be described by a pair of indices $i < j$ such that the substring $s[i \dots j]$ consists of two identical strings written after each other. ...
Finding repetitions
--- title suffix_array --- # Suffix Array ## Definition Let $s$ be a string of length $n$. The $i$-th suffix of $s$ is the substring $s[i \ldots n - 1]$. A **suffix array** will contain integers that represent the **starting indexes** of the all the suffixes of a given string, after the aforementioned suffixes are...
--- title suffix_array --- # Suffix Array ## Definition Let $s$ be a string of length $n$. The $i$-th suffix of $s$ is the substring $s[i \ldots n - 1]$. A **suffix array** will contain integers that represent the **starting indexes** of the all the suffixes of a given string, after the aforementioned suffixes are...
Suffix Array
--- title z_function --- # Z-function and its calculation Suppose we are given a string $s$ of length $n$. The **Z-function** for this string is an array of length $n$ where the $i$-th element is equal to the greatest number of characters starting from the position $i$ that coincide with the first characters of $s$....
--- title z_function --- # Z-function and its calculation Suppose we are given a string $s$ of length $n$. The **Z-function** for this string is an array of length $n$ where the $i$-th element is equal to the greatest number of characters starting from the position $i$ that coincide with the first characters of $s$....
Z-function and its calculation
--- title rabin_karp --- # Rabin-Karp Algorithm for string matching This algorithm is based on the concept of hashing, so if you are not familiar with string hashing, refer to the [string hashing](string-hashing.md) article. This algorithm was authored by Rabin and Karp in 1987. Problem: Given two strings - a pat...
--- title rabin_karp --- # Rabin-Karp Algorithm for string matching This algorithm is based on the concept of hashing, so if you are not familiar with string hashing, refer to the [string hashing](string-hashing.md) article. This algorithm was authored by Rabin and Karp in 1987. Problem: Given two strings - a pat...
Rabin-Karp Algorithm for string matching
--- title aho_corasick --- # Aho-Corasick algorithm The Aho-Corasick algorithm allows us to quickly search for multiple patterns in a text. The set of pattern strings is also called a _dictionary_. We will denote the total length of its constituent strings by $m$ and the size of the alphabet by $k$. The algorithm co...
--- title aho_corasick --- # Aho-Corasick algorithm The Aho-Corasick algorithm allows us to quickly search for multiple patterns in a text. The set of pattern strings is also called a _dictionary_. We will denote the total length of its constituent strings by $m$ and the size of the alphabet by $k$. The algorithm co...
Aho-Corasick algorithm
--- title matrix_rank --- # Finding the rank of a matrix **The rank of a matrix** is the largest number of linearly independent rows/columns of the matrix. The rank is not only defined for square matrices. The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix. Let the m...
--- title matrix_rank --- # Finding the rank of a matrix **The rank of a matrix** is the largest number of linearly independent rows/columns of the matrix. The rank is not only defined for square matrices. The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix. Let the m...
Finding the rank of a matrix
--- title determinant_gauss --- # Calculating the determinant of a matrix by Gauss Problem: Given a matrix $A$ of size $N \times N$. Compute its determinant. ## Algorithm We use the ideas of [Gauss method for solving systems of linear equations](linear-system-gauss.md) We will perform the same steps as in the sol...
--- title determinant_gauss --- # Calculating the determinant of a matrix by Gauss Problem: Given a matrix $A$ of size $N \times N$. Compute its determinant. ## Algorithm We use the ideas of [Gauss method for solving systems of linear equations](linear-system-gauss.md) We will perform the same steps as in the sol...
Calculating the determinant of a matrix by Gauss
--- title linear_systems_gauss --- # Gauss method for solving system of linear equations Given a system of $n$ linear algebraic equations (SLAE) with $m$ unknowns. You are asked to solve the system: to determine if it has no solution, exactly one solution or infinite number of solutions. And in case it has at least ...
--- title linear_systems_gauss --- # Gauss method for solving system of linear equations Given a system of $n$ linear algebraic equations (SLAE) with $m$ unknowns. You are asked to solve the system: to determine if it has no solution, exactly one solution or infinite number of solutions. And in case it has at least ...
Gauss method for solving system of linear equations
--- title: Calculating the determinant using Kraut method title - Original --- # Calculating the determinant using Kraut method in $O(N^3)$ In this article, we'll describe how to find the determinant of the matrix using Kraut method, which works in $O(N^3)$. The Kraut algorithm finds decomposition of matrix $A$ as ...
--- title: Calculating the determinant using Kraut method title - Original --- # Calculating the determinant using Kraut method in $O(N^3)$ In this article, we'll describe how to find the determinant of the matrix using Kraut method, which works in $O(N^3)$. The Kraut algorithm finds decomposition of matrix $A$ as ...
Calculating the determinant using Kraut method in $O(N^3)$
--- title sprague_grundy --- # Sprague-Grundy theorem. Nim ## Introduction This theorem describes the so-called **impartial** two-player game, i.e. those in which the available moves and winning/losing depends only on the state of the game. In other words, the only difference between the two players is that one of ...
--- title sprague_grundy --- # Sprague-Grundy theorem. Nim ## Introduction This theorem describes the so-called **impartial** two-player game, i.e. those in which the available moves and winning/losing depends only on the state of the game. In other words, the only difference between the two players is that one of ...
Sprague-Grundy theorem. Nim
--- title games_on_graphs --- # Games on arbitrary graphs Let a game be played by two players on an arbitrary graph $G$. I.e. the current state of the game is a certain vertex. The players perform moves by turns, and move from the current vertex to an adjacent vertex using a connecting edge. Depending on the game, t...
--- title games_on_graphs --- # Games on arbitrary graphs Let a game be played by two players on an arbitrary graph $G$. I.e. the current state of the game is a certain vertex. The players perform moves by turns, and move from the current vertex to an adjacent vertex using a connecting edge. Depending on the game, t...
Games on arbitrary graphs
--- title - Original --- # Binary search **Binary search** is a method that allows for quicker search of something by splitting the search interval into two. Its most common application is searching values in sorted arrays, however the splitting idea is crucial in many other typical tasks. ## Search in sorted ar...
--- title - Original --- # Binary search **Binary search** is a method that allows for quicker search of something by splitting the search interval into two. Its most common application is searching values in sorted arrays, however the splitting idea is crucial in many other typical tasks. ## Search in sorted ar...
Binary search
--- title ternary_search --- # Ternary Search We are given a function $f(x)$ which is unimodal on an interval $[l, r]$. By unimodal function, we mean one of two behaviors of the function: 1. The function strictly increases first, reaches a maximum (at a single point or over an interval), and then strictly decreas...
--- title ternary_search --- # Ternary Search We are given a function $f(x)$ which is unimodal on an interval $[l, r]$. By unimodal function, we mean one of two behaviors of the function: 1. The function strictly increases first, reaches a maximum (at a single point or over an interval), and then strictly decreas...
Ternary Search
--- title roots_newton --- # Newton's method for finding roots This is an iterative method invented by Isaac Newton around 1664. However, this method is also sometimes called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was published much earlier. The task...
--- title roots_newton --- # Newton's method for finding roots This is an iterative method invented by Isaac Newton around 1664. However, this method is also sometimes called the Raphson method, since Raphson invented the same algorithm a few years after Newton, but his article was published much earlier. The task...
Newton's method for finding roots
--- title simpson_integrating --- # Integration by Simpson's formula We are going to calculate the value of a definite integral $$\int_a ^ b f (x) dx$$ The solution described here was published in one of the dissertations of **Thomas Simpson** in 1743. ## Simpson's formula Let $n$ be some natural number. We divi...
--- title simpson_integrating --- # Integration by Simpson's formula We are going to calculate the value of a definite integral $$\int_a ^ b f (x) dx$$ The solution described here was published in one of the dissertations of **Thomas Simpson** in 1743. ## Simpson's formula Let $n$ be some natural number. We divi...
Integration by Simpson's formula