Competitive Programming Cheatsheet, Study Guides, Projects, Research of Computer science

Competitive Programming Cheatsheet

Typology: Study Guides, Projects, Research

2023/2024

Uploaded on 01/21/2025

unknown user
unknown user 🇧🇬

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ACM/ICPC CheatSheet
Puzzles
Contents
1 STL Useful Tips 2
1.1 Commonlibraries................................................................ 2
1.2 I/O........................................................................ 2
1.3 Usefulconstant................................................................. 2
1.4 Spacewaster .................................................................. 3
1.5 Tricksincmath................................................................. 3
1.6 Initialize array with predefined value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Modifyingsequenceoperations ........................................................ 3
1.8 Merge ...................................................................... 3
1.9 String ...................................................................... 4
1.10Heap....................................................................... 4
1.11Sort ....................................................................... 5
1.12Permutations .................................................................. 5
1.13Searching .................................................................... 6
1.14Randomalgorithm............................................................... 6
2 Number Theory 6
2.1 Primenumberunder100............................................................ 6
2.2 Maxormin ................................................................... 6
2.3 Greatestcommondivisor—GCD ...................................................... 6
2.4 LeastcommonmultipleLCM ....................................................... 6
2.5 Ifprimenumber ................................................................ 6
2.6 Primefactorization............................................................... 7
2.7 Leapyear .................................................................... 7
2.8 Binaryexponiential............................................................... 7
2.9 abmod p..................................................................... 8
2.10 Factorial mod .................................................................. 8
2.11Generatecombinations............................................................. 8
2.12 10-ary to m-ary................................................................. 9
2.13 m-aryto10-ary................................................................. 9
2.14Binomialcoefficient............................................................... 9
2.15Catalannumbers ................................................................ 10
2.16Euleriannumbers................................................................ 10
2.17KaratsubaalgorithminJava ......................................................... 10
2.18 Euler’s totient function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.19Splitplane.................................................................... 11
3 Searching Algorithms 11
3.1 Find rank kinarray .............................................................. 11
3.2 KMPAlgorithm ................................................................ 12
4 Dynamic Programming 13
4.1 0/1Knapsackproblems ............................................................ 13
4.2 CompleteKnapsackproblems......................................................... 13
4.3 Longestcommonsubsequence(LCS) ..................................................... 14
4.4 Longest increasing common sequence (LICS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.5 Longest Increasing Subsequence (LIS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Maximumsubmatrix.............................................................. 15
4.7 Partitionsofintegers.............................................................. 16
4.8 Partitionsofsets ................................................................ 17
5 Trees 17
5.1 Treetraversal.................................................................. 17
5.2 Depthandwidthoftree ............................................................ 18
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Competitive Programming Cheatsheet and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

ACM/ICPC CheatSheet

Puzzles

  • 1 STL Useful Tips Contents
    • 1.1 Common libraries
    • 1.2 I/O
    • 1.3 Useful constant
    • 1.4 Space waster
    • 1.5 Tricks in cmath
    • 1.6 Initialize array with predefined value
    • 1.7 Modifying sequence operations
    • 1.8 Merge
    • 1.9 String
    • 1.10 Heap
    • 1.11 Sort
    • 1.12 Permutations
    • 1.13 Searching
    • 1.14 Random algorithm
  • 2 Number Theory
    • 2.1 Prime number under
    • 2.2 Max or min
    • 2.3 Greatest common divisor — GCD
    • 2.4 Least common multiple — LCM
    • 2.5 If prime number
    • 2.6 Prime factorization
    • 2.7 Leap year
    • 2.8 Binary exponiential
    • 2.9 ab mod p
    • 2.10 Factorial mod
    • 2.11 Generate combinations
    • 2.12 10-ary to m-ary
    • 2.13 m-ary to 10-ary
    • 2.14 Binomial coefficient
    • 2.15 Catalan numbers
    • 2.16 Eulerian numbers
    • 2.17 Karatsuba algorithm in Java
    • 2.18 Euler’s totient function
    • 2.19 Split plane
  • 3 Searching Algorithms
    • 3.1 Find rank k in array
    • 3.2 KMP Algorithm
  • 4 Dynamic Programming
    • 4.1 0/1 Knapsack problems
    • 4.2 Complete Knapsack problems
    • 4.3 Longest common subsequence (LCS)
    • 4.4 Longest increasing common sequence (LICS)
    • 4.5 Longest Increasing Subsequence (LIS)
    • 4.6 Maximum submatrix
    • 4.7 Partitions of integers
    • 4.8 Partitions of sets
  • 5 Trees
    • 5.1 Tree traversal
    • 5.2 Depth and width of tree

6 Graph Theory 19 6.1 Graph representation.............................................................. 19 6.2 Flood fill algorithm............................................................... 21 6.3 SPFA — shortest path............................................................. 21 6.4 Floyd-Warshall algorithm – shortest path of all pairs............................................ 22 6.5 Prim — minimum spanning tree....................................................... 23 6.6 Eulerian circuit................................................................. 23 6.7 Topological sort................................................................. 24

1 STL Useful Tips

1.1 Common libraries

/*** Functions / #include #include // for hash #include // all useful constants #include #include #include // random #include #include #include #include // right justifying std::right and std::setw(width) / Data Structure ***/ #include // double ended queue #include #include // including priority_queue #include #include #include

1.2 I/O

// iostream and cstdio are both using I/O streams // However, they have different behavior, // pay attention on them if you’re using them together.

// cin does not concern with ’\n’ at end of each line // however scanf or getline does concern with ’\n’ at end of each line // ’\n’ will be ignored when you use cin to read char.

// when you use getline(cin, str) to read a whole line of input // please add an extra getline before inputing if previous inputs are numbers cin >> n; getline(cin, str) // wasted getline getline(cin, str) // real input string

1.3 Useful constant

INT_MIN

INT_MAX

LONG_MIN

LONG_MAX

LLONG_MIN

LLONG_MAX

(~0u) // infinity (for long and long long) // use (~0u)>>2 for int.

// difference of two sorted ranges void set_difference((first1, last1, first2, last2, result, comp);

1.9 String

// Searching unsigned int find(const string &s2, unsigned int pos1 = 0); unsigned int rfind(const string &s2, unsigned int pos1 = end); unsigned int find_first_of(const string &s2, unsigned int pos1 = 0); unsigned int find_last_of(const string &s2, unsigned int pos1 = end); unsigned int find_first_not_of(const string &s2, unsigned int pos1 = 0); unsigned int find_last_not_of(const string &s2, unsigned int pos1 = end); // Insert, Erase, Replace string& insert(unsigned int pos1, const string &s2); string& insert(unsigned int pos1, unsigned int repetitions, char c); string& erase(unsigned int pos = 0, unsigned int len = npos); string& replace(unsigned int pos1, unsigned int len1, const string &s2); string& replace(unsigned int pos1, unsigned int len1, unsigned int repetitions, char c); // String streams stringstream s1; int i = 22; s1 << "Hello world! " << i; cout << s1.str() << endl;

1.10 Heap

template void push_heap (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void push_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

template void pop_heap (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void pop_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

template void make_heap (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp );

template void sort_heap (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void sort_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp); template RandomAccessIterator is_heap_until (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> RandomAccessIterator is_heap_until (RandomAccessIterator first, RandomAccessIterator last Compare comp);

1.11 Sort

void sort(iterator first, iterator last); void sort(iterator first, iterator last, LessThanFunction comp); void stable_sort(iterator first, iterator last); void stable_sort(iterator first, iterator last, LessThanFunction comp); void partial_sort(iterator first, iterator middle, iterator last); void partial_sort(iterator first, iterator middle, iterator last, LessThanFunction comp); bool is_sorted(iterator first, iterator last); bool is_sorted(iterator first, iterator last, LessThanOrEqualFunction comp); // example for sort, if have array x, start_index, end_index; sort(x+start_index, x+end_index);

/** sort a map */ // You cannot directly sort a map<key type, mapped data type> // if you only want to sort in key type // you can use insert method to copy map into another map // b.insert(make_pair(it->first, it->second) / it is a map iterator */ // this will result a map which sorts key type in increasing order // if you want to sort key type in decreasing order, then declare your map as // something like: // map<char, int, greater >

// if you want to sort based on key, you need to copy the data to a vector // where elements of vector are pair. // you can define a PAIR type by using: typedef pair<char, int> PAIR;

// suppose this is the map map<char, int> a;

// sort vector in decreasing order bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) { return lhs.second > rhs.second; }

// sort key in increasing order bool cmp_by_char(const PAIR& lhs, const PAIR& rhs) { return lhs.first < rhs.first; }

// copy map data to vector vector b(a.begin(), a.end());

// sort data sort(b.begin(), b.end(), cmp_by_value);

// you can still call your data by b[i].first and b[i].second. // THE ABOVE CODES ARE EXAMPLE FOR SORTING A MAP. // PLEASE USE IT FOR YOUR OWN DEMANDS.

1.12 Permutations

bool next_permutation(iterator first, iterator last); bool next_permutation(iterator first, iterator last, LessThanOrEqualFunction comp); bool prev_permutation(iterator first, iterator last); bool prev_permutation(iterator first, iterator last, LessThanOrEqualFunction comp);

if (n<=3) return true; if (!(n%2) || !(n%3)) return false; for (int i=5;i*i<=n;i+=6) if (!(n%i) || !(n%(i+2))) return false;

return true; }

2.6 Prime factorization

// smallest prime factor of a number. function factor(int n) { int a; if (n%2==0) return 2 ; for (a=3;a<=sqrt(n);a++++) { if (n%a==0) return a; } return n; }

// complete factorization int r; while (n>1) { r = factor(n); printf("%d", r); n /= r; }

2.7 Leap year

bool isLeap(int n) { if (n%100==0) if (n%400==0) return true; else return false;

if (n%4==0) return true; else return false; }

2.8 Binary exponiential

int binpow (int a, int n) { int res = 1; while (n) if (n & 1) { res *= a; --n; }

else { a *= a; n >>= 1; } return res; }

2.9 ab^ mod p

long powmod(long base, long exp, long modulus) { base %=^ modulus; long result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; }

2.10 Factorial mod

//n! mod p int factmod (int n, int p) { long long res = 1; while (n > 1) { res = (res * powmod (p-1, n/p, p)) % p; for (int i=2; i<=n%p; ++i) res=(res*i) %p; n /= p; } return int (res % p); }

2.11 Generate combinations

// n>=m, choose M numbers from 1 to N. void combination(int n, int m) { if (n<m) return ; int a[ 50 ]={ 0 }; int k=0;

for (int i=1;i<=m;i++) a[i]=i; while (true) { for (int i=1;i<=m;i++) cout << a[i] << " "; cout << endl;

k=m; while ((k>0) && (n-a[k]==m-k)) k--; if (k==0) break; a[k]++; for (int i=k+1;i<=m;i++)

2.15 Catalan numbers

Cn =

n∑− 1

k=

CkCn− 1 −k =

n + 1

n k

The first terms of this sequence are 2, 5, 14, 42, 132, 429, 1430 when C 0 = 1. This is the number of ways to build a balanced formula from n sets of left and right parentheses. It is also the number of triangulations of a convex polygon, the number of rooted binary tress on n + 1 leaves and the number of paths across a lattice which do not rise above the main diagonal.

2.16 Eulerian numbers

n k

= k

n − 1 k

  • (n − k + 1)

n − 1 k − 1

// This is the number of permutations of length n with exactly k ascending sequences or runs. // Basis: k=0 has value 1 #define MAXN 100 // largest n or k long eularian(n,k) int n,m; { int i,j; long e[MAXN][MAXN]; for (i=0; i<=n; i++) e[i][ 0 ] = 1; for (j=0; j<=n; j++) e[ 0 ][j] = 0; for (i=1; i<=n; i++) for (j=1; j<i; j++) e[i][j] = ke[i-1][j] + (i-j+1)e[i-1][j-1]; return e[n][k]; }

2.17 Karatsuba algorithm in Java

// fast algorithm to find multiplication of two big numbers. import java.math.BigInteger; import java.util.Random;

class Karatsuba { private final static BigInteger ZERO = new BigInteger("0"); public static BigInteger karatsuba(BigInteger x, BigInteger y) { int N = Math.max(x.bitLength(), y.bitLength()); if (N <= 2000) return x.multiply(y); N=(N/2)+(N %2); BigInteger b = x.shiftRight(N); BigInteger a = x.subtract(b.shiftLeft(N)); BigInteger d = y.shiftRight(N); BigInteger c = y.subtract(d.shiftLeft(N)); BigInteger ac = karatsuba(a, c); BigInteger bd = karatsuba(b, d); BigInteger abcd = karatsuba(a.add(b), c.add(d)); return ac.add(abcd.subtract(ac).subtract(bd).shiftLeft(N)).add(bd.shiftLeft(2*N)); }

public static void main(String[] args) { long start, stop, elapsed; Random random = new Random();

int N = Integer.parseInt(args[0]); BigInteger a = new BigInteger(N, random); BigInteger b = new BigInteger(N, random); start = System.currentTimeMillis(); BigInteger c = karatsuba(a, b); stop = System.currentTimeMillis(); System.out.println(stop - start); start = System.currentTimeMillis(); BigInteger d = a.multiply(b); stop = System.currentTimeMillis(); System.out.println(stop - start); System.out.println((c.equals(d))); } }

2.18 Euler’s totient function

// the positive integers less than or equal to n that are relatively prime to n. int phi (int n) { int result = n; for (int i=2; i*i<=n; ++i) if(n %i==0) { while(n %i==0) n /= i; result -= result / i; } if (n > 1) result -= result / n; return result; }

2.19 Split plane

n lines can split a plane in (n+1) 2 n+ 1 sub-regions.

3 Searching Algorithms

3.1 Find rank k in array

int find(int l, int r, int k) { int i=0,j=0,x=0,t=0;

if (l==r) return a[l]; x=a[(l+r)/2]; t=a[x]; a[x]=a[r]; a[r]=t; i=l-1;

for (int j=l; j<=r-1;j++) if (a[j]<=a[r]) { i++; t=a[i]; a[i]=a[j]; a[j]=t; } i++;

"little needs to be done in leaving it. The only minor complication is that the "+ "logic which is correct late in the string erroneously gives non-proper "+ "substrings at the beginning. This necessitates some initialization code.";

string b = "table";

int p = KMP(a, b); cout << p << ": " << a.substr(p, b.length()) << " " << b << endl;

return 0 ; }

4 Dynamic Programming

4.1 0/1 Knapsack problems

#include

using namespace std;

int f[ 1000 ]={ 0 }; int n=0, m=0;

int main(void) { cin >> n >> m;

for (int i=1;i<=n;i++) { int price=0, value=0; cin >> price >> value;

for (int j=m;j>=price;j--) if (f[j-price]+value>f[j]) f[j]=f[j-price]+value; }

cout << f[m] << endl;

return 0 ; }

4.2 Complete Knapsack problems

#include

using namespace std;

int f[ 1000 ]={ 0 }; int n=0, m=0;

int main(void) { cin >> n >> m;

for (int i=1;i<=n;i++) {

int price=0, value=0; cin >> price >> value;

for (int j=price; j<=m; j++) if (f[j-price]+value>f[j]) f[j]=f[j-price]+value; }

cout << f[m] << endl;

return 0 ; }

4.3 Longest common subsequence (LCS)

int dp[ 1001 ][ 1001 ];

int lcs(const string &s, const string &t) { int m = s.size(), n = t.size(); if (m == 0 || n == 0) return 0 ; for (int i=0; i<=m; ++i) dp[i][ 0 ] = 0; for (int j=1; j<=n; ++j) dp[ 0 ][j] = 0; for (int i=0; i<m; ++i) for (int j=0; j<n; ++j) if (s[i] == t[j]) dp[i+1][j+1] = dp[i][j]+1; else dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]); return dp[m][n]; }

4.4 Longest increasing common sequence (LICS)

#include

using namespace std;

int a[ 100 ]={ 0 }; int b[ 100 ]={ 0 }; int f[ 100 ]={ 0 }; int n=0, m=0;

int main(void) { cin >> n; for (int i=1;i<=n;i++) cin >> a[i]; cin >> m; for (int i=1;i<=m;i++) cin >> b[i];

for (int i=1;i<=n;i++) { int k=0; for (int j=1;j<=m;j++) {

// URAL 1146 Maximum Sum #include

using namespace std;

int a[ 150 ][ 150 ]={ 0 }; int c[ 200 ]={ 0 };

int maxarray(int n) { int b=0, sum=-100000000; for (int i=1;i<=n;i++) { if (b>0) b+=c[i]; else b=c[i]; if (b>sum) sum=b; }

return sum; }

int maxmatrix(int n) { int sum=-100000000, max=0;

for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) c[j]=0;

for (int j=i;j<=n;j++) { for (int k=1;k<=n;k++) c[k]+=a[j][k]; max=maxarray(n); if (max>sum) sum=max; } }

return sum; }

int main(void) { int n=0; cin >> n; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) cin >> a[i][j];

cout << maxmatrix(n); return 0 ; }

4.7 Partitions of integers

#define MAXN 100 // largest n or m long int_coefficient(n,k) // compute f(n,k)

int n,m; { int i,j; long f[[MAXN][MAXN]; f [ 1 ][ 1 ] = 1; for (i=0;i<=n;i++) f[i][ 0 ] = 0; for (i=1; i<=n; i++) for (j=1; j<i; j++) if (i-j <= 0) f[i][j] = f[i][k-1]; else f[i][j] = f[i-j][k]+f[i][k-1]; return f[n][k]; }

4.8 Partitions of sets

Number of ways to partition n + 1 items into k sets.

{ n k

= k

n − 1 k

n − 1 k − 1

where (^) { n 1

n n

5 Trees

5.1 Tree traversal

int L[ 100 ]={ 0 }; int R[ 100 ]={ 0 };

void DLR(int m) { cout << m << " "; if (L[m]!=0) DLR(L[m]); if (R[m]!=0) DLR(R[m]); }

void LDR(int m) { if (L[m]!=0) LDR(L[m]); cout << m << " "; if (R[m]!=0) LDR(R[m]); }

void LRD(int m) { if (L[m]!=0) LRD(L[m]); if (R[m]!=0) LRD(R[m]); cout << m << " "; }

int main(void) { cin >> n; for (int i=1;i<=n;i++) cin >> L[i] >> R[i];

int main(void) { cin >> n;

for (int i=1;i<=n;i++) cin >> l[i] >> r[i];

d=depth( 1 ); mystack.push( 1 ); width( 1 );

cout << w << " " << d << endl;

return 0 ; }

6 Graph Theory

6.1 Graph representation

// The most common way to define graph is to use adjacency matrix // example: // (1) (2) (3) (4) (5) // (1) 2 0 5 0 0 // (2) 4 2 0 0 1 // (3) 3 0 0 1 4 // (4) 6 9 0 0 0 // (5) 1 1 1 1 5 // it’s always a square matrix. // suppose a graph has n nodes, if given exactly adjacency matrix for (int i=1;i<=n;i++) for (int j=1;i<=n;j++) { cin << a[i][j] << endl; } // Usually will go like this representation in data // start_node end_node weight // suppose m lines for (int i=1;i<=m;i++) { int x=0, y=0, t=0; cin >> x >> y >> t; a[x][y]=t; // if undirected graph a[y][x]=t; } // another variant: on the ith line, has data as // end_node weight // when you read data, you can assign matrix as a[i][x]=t; // if undirected graph a[x][i]=t;

// Initialization of graph !!!IMPORTANT // Depends on usage, normally initialize as 0 for all elements in matrix. // so that 0 means no connection, non-0 means connection // (for problem without weight, use weight as 1) // If weights are important in this context (especially searching for path)

// Initialize graph as infinity for all elements in matrix.

// Another way to store graph is Adjacency list // No space advantage if using array (unknown maximum number for in-degree). // Big space advantage if using dynamic data structure (like list, vector). // each row represent a node and its connectivity. // we don’t need it so much due to it’s search efficiency. // let’s define a node as struct Node{ int id; // node id int w; // weight }; // suppose n nodes and m lines of inputs as // start_node end_node weight // assume using in this example // g is a vector, and each element of g is also a vector of Node for (int i=1;i<=m;i++) { int x=0, y=0, t=0; cin >> x >> y >> t; Node temp; temp.id=y; temp.w=t; g[x].push_back(temp); // if undirected temp.id=x; g[y].push_back(temp); } // Note that you don’t need this node structure if graph has only connectivity information.

/**** Special Structure ****/

// Special structure here is usually not a typical graph, like city-blocks, triangles // They are represented in 2-d array and shows weights on nodes instead of edges. // Note that in this case travel through edge has no cost, but visit node has cost.

// Triangles: Read data like this // 1 // 1 2 // 4 2 7 // 7 3 1 5 // 6 2 9 4 6 for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) cin >> a[i][j];

// Simple city-blocks: it’s just like first form of adjacency matrix, but this time // represents weights on nodes, may not be square matrix. // 1 2 4 5 6 // 2 4 5 1 3 // 4 5 2 3 6 for (int i=1;i<=n;i++) for (int j=1;<=m;j++) cin >> a[i][j];

// More complex data structures: typical city-block structure may has some constraints on // questions, but it has no boundaries. However, some questions requires to form a maze. // In these cases, data structures can be very flexible, it totally depends on how the question // presents the data. A usual way is to record it’s adjacent blocks information: struct Block{ bool l[ 4 ]; // if has 8 neighbors then use bool l[8];