Data Structures and Algorithms Implementation with C++
- STDIO
cout << fixed << setprecision(10)
- Language
int
capacity:-2 * 10^9
to2 * 10^9
long long int
capacity:-9 * 10^18
to9 * 10^18
- Global Array Size:
10^7
- Local Array Size:
10^5
typeid
operator in c++
- OJ Time Limits
- Number of iterations in 1 second is :
10^7 to 10^8
10^8
operation ->1second
10^9 -> 10 seconds
10^10
operation ->10^10/10^8
or100seconds
10^11 -> 1000 seconds
- Number of iterations in 1 second is :
- Primes and Divisors
log(a^b) = b log(a)
n << k
->n * 2^k
n >> k
->n / 2^k
- 2'complement of
~N
=-((~(~N))+1)
=-(N+1)
- positive integers:
>= 1
and non-negative integers:>=0
log6(x) = log_e(x) / log_e(6)
- builtin functions
__builtin_popcount(x)
__builtin_clz(x)
__builtin_ctz(x)
- xor Trick
n ^ (n + 1) == 1 // if n is even
x ^ 0 == x
x ^ y == 0 // x == y
x ^ x ^ x = x // len even
x ^ x ^ x ^ x = 0 // len odd
- BIT LOW: Think about each bit separately. That's it. It will make your life real comfy.
- The complexity of bitwise operations in bitset of size is O(size/32) or O(size/64)
- Modular Arithmetic
- Notation:
- expr1 ≡ expr2(mod m). This is read as "expr1 is congruent to expr2 modulo m", and is shorthand for expr1 mod m=expr2 mod m.
- Basic arithmetics:
-b % m
=(m - b) % m
-4 % 6
=(6 - 4) % 6
-49 % 6
=((-47 % 6) + 6) % 6
- finally
-b % m
=((-b % m) + m) % m
a % b
=a - (floor(a/b) * b)
(a + b) % m
=(a % m + b % m) % m
(a - b) % m
=(a % m - b % m) % m
(a * b) % m
=(a % m * b % m) % m
25 ^ 4 % 10
=(25 % 10 * 25 % 10 * 25 % 10 * 25 % 10) % 10
=(5 * 5 * 5 * 5) % 10
=(5 % 10 * 5 % 10 * 5 % 10 * 5 % 10) % 10
- modulo
2^32
and2^64
x % 10^k
got last k digitx % 2^k
got last k bitx % 2^k
=x & (2^k - 1)
- Note that
x % 2^k
=x & (2^k - 1)
. Sounsigned int x, y, z; z = x * y
is the same asz = 1LL * x * y % 2^32
. Similarlyunsigned long long
will automatically take modulo by2^64
.
- modular multiplicative inverse
- mmi defined when
a
andm
is coprime - if
gcd(a, m) == 1
thena
andm
is coprime (a / b) % m
=>(a * b^-1) % m
=>(a % m) * (b^-1 % m) % m
b^-1 % m
- modular multiplicative inverse ofb
(a * b) % m = 1
-b
is mmi ofa
=>((a % m) * (b % m)) % m = 1
- mmi defined when
- fermat's little theorem
a ^ m-1 = 1 % m
-> fermat's little theorem, herem
is prime anda
is not multiple ofm
a^-1 % m = a^m-2
=>a^-1 = a^m-2 % m
a / b % m
=a * b^m-2 % m
ifm
is prime
- intuitive mod
- x mod m < m / 2 when x >= m.
- Notation:
- Functions
- Min/Max:
min()
,max()
,min_element()
,max_element()
- Binary Search:
binary_search()
,upper_bound()
,lower_bound()
- Modifying:
reverse()
,swap()
,unique()
- Non-modifying:
count()
- Sorting:
sort()
- Min/Max:
- time complexity
- space complexity
- constant
O(1)
- logarithmic
O(log n)
- linear
O(n)
- quadratic
O(n^2)
- cubic
O(n^3)
- linearithmic
O(n log n)
- exponential
O(2^n)
- factorial
O(n!)
-
O(sqrt(n))
- print 1 to n
- print n to 1
- sum of array
- sum of n number
- digit sum
- fibonacci
- gcd with recursion
- calculate x of the power y
- reverse string
- pair
- vector
- iterator
- string, stringstream
- map, unordered_map
- set, unordered_set, multiset
- bitset
- math.h
- algorithms
- stack
- queue, deque, priority_queue
- selection sort
- bubble sort
- insertion sort
- counting sort
- merge sort
- lexicographical sorting
- std::sort()
- comparator function
- quick sort
- linear search
- binary search
- lower bound
- upper bound
- peak element
- first last occurrence
- rotated array
- max in a hill
- square root
- divisors
- gcd lcm
- primality test
- prime factors
- sieve of eratosthenes
- string multiplication
- base conversion
- bitwise operators
- k-th bit on/off and set/unset
- bit masking
- builtin functions
- xor trick
- bitset
- contribution technique
- basic arithmetics
- overflow
- bigmod (binary exponentiation)
- modular inverse (modular multiplicative inverse)
- mulmod
- factorial with mod
- stack
- queue
- circular queue
- deque
- singly linked list
- priority queue
- adjacency matrix
- adjacency list
- adjacency matrix with edge weight
- adjacency list with edge weight
- dfs (depth first search)