Competitive Programming Notes
Complexity Guidelines
| order | Common constraints | Common algorithms |
|---|---|---|
| Exhaustive search | ||
| Binary search, sorting | ||
| Exhaustive search | ||
| Meet in the middle + binary search | ||
| Exhaustive search | ||
| Meet in the middle | ||
| Bit exhaustive search, bit DP | ||
| Permutations, combinations |
Array Size Limits
Approximately is the limit. does not cause an error (in Rust). If the size is too large, consider coordinate compression.
Set Operation Complexity
| Operation | Average complexity |
|---|---|
| s | l | |
| s & l | |
| s - l |
Harmonic Series Complexity
This complexity appears when incrementing multiples for primality testing, among other cases.
- ABC170-D
- ABC172-D
- ABC177-E
The complexity of enumerating all pairs where is .
floor mod
Therefore, we can subtract using an arbitrary integer k from . This is equal to . So,
can compute the result in .
- ARC111-A
Tree Conditions
When the number of vertices is , the number of edges is .
Floating Point Errors
This kind of thing can happen, so add 0.5 for rounding.
Bit Operations
Bit operations (&, |, !, ^) often become clearer when considered digit by digit.
Determining Whether the x-th Digit is 0 or 1
Expected Value
The expected number of trials (including the final successful one) until success, when each trial succeeds with probability , is .
Coupon Collector's Problem
Conditions for Valid Parentheses
These are examples of valid parentheses. The condition is: scanning from left to right, if we let left be the count of '(' and right be the count of ')', then must always hold, and ultimately .
Miscellaneous Tips
- When there are 3 points, fix the middle one.
- When the answer is small, think from the answer's perspective.
- When
x + y && x - yappears, consider a 45-degree rotation. |x| = max(x, -x)- When numbers are large, take the modulus.
- For
gcd(m, 10), for example"123"can be expressed as1*10**2 % m + 2*10**1 % m + 3*10**0 % m. - If there is periodicity, consider modular arithmetic. If there is a modulus, consider periodicity.
- A linear Diophantine equation can be solved using the extended Euclidean algorithm.
- When considering the modulus of large numbers, try expressing the n-th digit as (when ).
- Digit DP can often be used when counting numbers with certain properties that are at most N.
- For lexicographically smallest, use a greedy approach from the front!
- In counting problems, reversing the counting order sometimes works well.