\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{yidne} \pdfinfo{ /Title (icpc-compitative-programming-cheat-sheet.pdf) /Creator (Cheatography) /Author (yidne) /Subject (ICPC-compitative programming Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{101FA3} \definecolor{LightBackground}{HTML}{F0F1F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{ICPC-compitative programming Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{yidne} via \textcolor{DarkBackground}{\uline{cheatography.com/161061/cs/34260/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}yidne \\ \uline{cheatography.com/yidne} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 16th August, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Binary search}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# binary search \newline \newline def bs(arr, x): \newline l, h = 0, len(arr) - 1 \newline while l \textless{}= h: \newline mid = l +(h - l) // 2 \newline if arr{[}mid{]} \textless{} x: \newline l = mid + 1 \newline elif arr{[}mid{]} \textgreater{} x: \newline h = mid -1 \newline else: \newline return True \newline return False} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{maximum sub array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\# Kadane's Algorithm: O(n) \newline def kadanes(nums): \newline maxSum = nums{[}0{]} \newline curSum = 0 \newline \newline for n in nums: \newline curSum = max(curSum, 0) \newline curSum += n \newline maxSum = max(maxSum, curSum) \newline return maxSum \newline \newline \# Return the left and right index of the max subarray sum, \newline \# assuming there's exactly one result (no ties). \newline \# Sliding window variation of Kadane's: O(n) \newline def slidingWindow(nums): \newline maxSum = nums{[}0{]} \newline curSum = 0 \newline maxL, maxR = 0, 0 \newline L = 0 \newline \newline for R in range(len(nums)): \newline if curSum \textless{} 0: \newline curSum = 0 \newline L = R \newline \newline curSum += nums{[}R{]} \newline if curSum \textgreater{} maxSum: \newline maxSum = curSum \newline maxL, maxR = L, R \newline \newline return {[}maxL, maxR{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Generating subsets}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//method 1 \newline void search(int k) \{ \newline if (k == n) \{ \newline // process subset \newline \} else \{ \newline search(k+1); \newline subset.push\_back(k); \newline search(k+1); \newline subset.pop\_back(); \newline \} \newline \} \newline \newline //method 2 \newline for (int b = 0; b \textless{} (1\textless{}\textless{}n); b++) \{ \newline vector subset; \newline for (int i = 0; i \textless{} n; i++) \{ \newline if (b\&(1\textless{}\textless{}i)) subset.push\_back(i); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{method 1 - search generates the subsets of the set \{0, 1, . . . , n − 1\}. The \newline function maintains a vector subset that will contain the elements of each subset. \newline The search begins when the function is called with parameter 0. \newline \newline Method -2 shows how we can find the elements of a subset that \newline corresponds to a bit sequence. When processing each subset, the code builds a \newline vector that contains the elements in the subset} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Policy-based data structures}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\#include \textless{}ext/pb\_ds/assoc\_container.hpp\textgreater{} \newline using namespace \_\_gnu\_pbds; \newline \newline typedef tree\textless{}int,null\_type,less,rb\_tree\_tag, \newline tree\_order\_statistics\_node\_update\textgreater{} indexed\_set; \newline \newline indexed\_set s; \newline s.insert(2); \newline s.insert(3); \newline s.insert(7); \newline s.insert(9); \newline \newline auto x = s.find\_by\_order(2); \newline cout \textless{}\textless{} *x \textless{}\textless{} "\textbackslash{}n"; // 7 \newline cout \textless{}\textless{} s.order\_of\_key(7) \textless{}\textless{} "\textbackslash{}n"; // 2 \newline \newline //If the element does not appear in the set, we get the position that the element would have in the //set: \newline \newline cout \textless{}\textless{} s.order\_of\_key(6) \textless{}\textless{} "\textbackslash{}n"; // 2 \newline cout \textless{}\textless{} s.order\_of\_key(8) \textless{}\textless{} "\textbackslash{}n"; // 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Comparison functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//the following comparison function comp sorts \newline //strings primarily by length and secondarily by alphabetical order: \newline bool comp(string a, string b) \{ \newline if (a.size() != b.size()) return a.size() \textless{} b.size(); \newline return a \textless{} b; \newline \} \newline \newline //Now a vector of strings can be sorted as follows: \newline sort(v.begin(), v.end(), comp);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Comparison operators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{vector\textless{}pair\textless{}int,int\textgreater{}\textgreater{} v; \newline v.push\_back(\{1,5\}); \newline v.push\_back(\{2,3\}); \newline v.push\_back(\{1,2\}); \newline sort(v.begin(), v.end()); \newline \newline vector\textless{}tuple\textless{}int,int,int\textgreater{}\textgreater{} v; \newline v.push\_back(\{2,1,4\}); \newline v.push\_back(\{1,5,3\}); \newline v.push\_back(\{2,1,3\}); \newline sort(v.begin(), v.end());} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Pairs ( pair ) and tuples are sorted primarily according to their first elements ( first ). However, if the first elements of two pairs are equal, they are sorted according to \newline their second elements ( second ):} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Longest increasing subsequence}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{for (int k = 0; k \textless{} n; k++) \{ \newline length{[}k{]} = 1; \newline for (int i = 0; i \textless{} k; i++) \{ \newline if (array{[}i{]} \textless{} array{[}k{]}) \{ \newline length{[}k{]} = max(length{[}k{]},length{[}i{]}+1); \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{To calculate a value of length ( k ), we should find a position i \textless{} k for which \newline array {[} i {]} \textless{} array {[} k {]} and length ( i ) is as large as possible. Then we know that \newline length ( k ) = length ( i ) + 1, because this is an optimal way to add array {[} k {]} to a \newline subsequence. However, if there is no such position i , then length ( k ) = 1, which \newline means that the subsequence only contains array {[} k {]}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Bitset}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{bitset\textless{}10\textgreater{} s; \newline s{[}1{]} = 1; \newline s{[}3{]} = 1; \newline s{[}4{]} = 1; \newline s{[}7{]} = 1; \newline cout \textless{}\textless{} s{[}4{]} \textless{}\textless{} "\textbackslash{}n"; // 1 \newline cout \textless{}\textless{} s{[}5{]} \textless{}\textless{} "\textbackslash{}n"; // 0 \newline \newline bitset\textless{}10\textgreater{} s(string("0010011010")); // from right to left \newline cout \textless{}\textless{} s{[}4{]} \textless{}\textless{} "\textbackslash{}n"; // 1 \newline cout \textless{}\textless{} s{[}5{]} \textless{}\textless{} "\textbackslash{}n"; // 0 \newline \newline //count returns the number of one in the bit seet \newline bitset\textless{}10\textgreater{} s(string("0010011010")); \newline cout \textless{}\textless{} s.count() \textless{}\textless{} "\textbackslash{}n"; // 4 \newline \newline // using bit operation \newline bitset\textless{}10\textgreater{} a(string("0010110110")); \newline bitset\textless{}10\textgreater{} b(string("1011011000")); \newline cout \textless{}\textless{} (a\&b) \textless{}\textless{} "\textbackslash{}n"; // 0010010000 \newline cout \textless{}\textless{} (a|b) \textless{}\textless{} "\textbackslash{}n"; // 1011111110 \newline cout \textless{}\textless{} (a\textasciicircum{}b) \textless{}\textless{} "\textbackslash{}n"; // 1001101110} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Generating permutations of a set}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//ex \{0, 1, 2\} are (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0) \newline //Method 1 using recursion \newline void search() \{ \newline if (permutation.size() == n) \{ \newline // process permutation \newline \} else \{ \newline for (int i = 0; i \textless{} n; i++) \{ \newline if (chosen{[}i{]}) continue; \newline chosen{[}i{]} = true; \newline permutation.push\_back(i); \newline search(); \newline chosen{[}i{]} = false; \newline permutation.pop\_back(); \newline \} \newline \} \newline \} \newline \newline //method 2 using the c++ stl next\_permutation \newline vector permutation; \newline for (int i = 0; i \textless{} n; i++) \{ \newline permutation.push\_back(i); \newline \} \newline do \{ \newline // process permutation \newline \} while \seqsplit{(next\_permutation(permutation}.begin(),permutation.end()));} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Method - 1 - search goes through the permutations of the set \{0, 1, . . . , n − 1\}. The \newline function builds a vector permutation that contains the permutation, and the \newline search begins when the function is called without parameters. \newline Method - 2 - Another method for generating permutations is to begin with the permutation \newline \{0, 1, . . . , n − 1\} and repeatedly use a function that constructs the next permu- \newline tation in increasing order.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Diophantine equations}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ax + b y = gcd( a, b ) \newline % Row Count 1 (+ 1) A Diophantine equation can be solved if c is divisible by gcd( a, b ), and other- \newline % Row Count 3 (+ 2) wise it cannot be solved. \newline % Row Count 4 (+ 1) 39 x + 15 y = 12 \newline % Row Count 5 (+ 1) gcd(39, 15) = gcd(15, 9) = gcd(9, 6) = gcd(6, 3) = gcd(3, 0) = 3 \newline % Row Count 7 (+ 2) A solution to a Diophantine equation is not unique, because we can form an \newline % Row Count 9 (+ 2) infinite number of solutions if we know one solution. If a pair ( x, y ) is a solution, \newline % Row Count 11 (+ 2) then also all pairs \newline % Row Count 12 (+ 1) (x + kb/gcd(a,b), y - ka/gcd(a,b)) are solutions, where k is any integer.% Row Count 14 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Modular exponentiation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//The following function calculates the value of x n mod m : \newline int modpow(int x, int n, int m) \{ \newline if (n == 0) return 1\%m; \newline long long u = modpow(x,n/2,m); \newline u = (u{\emph{u)\%m; \newline if (n\%2 == 1) u = (u}}x)\%m; \newline return u; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Complex numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{typedef long long C; \newline typedef complex\textless{}C\textgreater{} p; \newline \#define X real() \newline \#define Y imag() \newline P p = \{4,2\}; \newline cout \textless{}\textless{} p.X \textless{}\textless{} " " \textless{}\textless{} p.Y \textless{}\textless{} "\textbackslash{}n"; // 4 2 \newline //The following code defines vectors v = (3, 1) and u = (2, 2), and after that \newline //calculates the sum s = v + u . \newline P v = \{3,1\}; \newline P u = \{2,2\}; \newline P s = v+u; \newline cout \textless{}\textless{} s.X \textless{}\textless{} " " \textless{}\textless{} s.Y \textless{}\textless{} "\textbackslash{}n"; // 5 3 \newline //The following code calculates the distance between points (4, 2) and (3, − 1): \newline //abs() == root(x\textasciicircum{}2 + y\textasciicircum{}2) \newline P a = \{4,2\}; \newline P b = \{3,-1\}; \newline \newline //The following code calculates the angle of the vector (4, 2), rotates it 1/2 \newline //radians counterclockwise, and then calculates the angle again: \newline //arg(v) used to calculate angle of a vector with respect to x \newline //polar(s, a) constructs a vector whose length is s and that points to an angle a. \newline \newline P v = \{4,2\}; \newline cout \textless{}\textless{} arg(v) \textless{}\textless{} "\textbackslash{}n"; // 0.463648 \newline v *= polar(1.0,0.5); \newline cout \textless{}\textless{} arg(v) \textless{}\textless{} "\textbackslash{}n"; // 0.963648 \newline \newline \newline cout \textless{}\textless{} abs(b-a) \textless{}\textless{} "\textbackslash{}n"; // 3.16228} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Points and lines}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{P a = \{4,2\}; \newline P b = \{1,2\}; \newline C p = (conj(a)*b).Y; // 6 \newline The above code works, because the function conj negates the y coordinate of \newline a vector, and when the vectors ( x 1 , − y 1 ) and ( x 2 , y 2 ) are multiplied together, the y \newline coordinate of the result is x 1 y 2 − x 2 y 1 .} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{The cross product a × b of vectors a = ( x 1 , y 1 ) and b = ( x 2 , y 2 ) is calculated using \newline the formula x 1 y 2 − x 2 y 1 . The cross product tells us whether b turns left (positive \newline value), does not turn (zero) or turns right (negative value) when it is placed \newline directly after a .} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Point distance from a line}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{( a − c ) × ( b − c ) / 2 \newline % Row Count 1 (+ 1) shortest distance between p, s1, s2 which are the vertex of a triangle is \newline % Row Count 3 (+ 2) d = ((s1 - p) * (s2 - p)) / |s2 - s1|% Row Count 4 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Area of polygon}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// C++ program to evaluate area of a polygon using \newline // shoelace formula \newline \#include \textless{}bits/stdc++.h\textgreater{} \newline using namespace std; \newline \newline // (X{[}i{]}, Y{[}i{]}) are coordinates of i'th point. \newline double polygonArea(double X{[}{]}, double Y{[}{]}, int n) \newline \{ \newline // Initialize area \newline double area = 0.0; \newline \newline // Calculate value of shoelace formula \newline int j = n - 1; \newline for (int i = 0; i \textless{} n; i++) \newline \{ \newline area += (X{[}j{]} + X{[}i{]}) * (Y{[}j{]} - Y{[}i{]}); \newline j = i; // j is previous vertex to i \newline \} \newline \newline // Return absolute value \newline return abs(area / 2.0); \newline \} \newline \newline // Driver program to test above function \newline int main() \newline \{ \newline double X{[}{]} = \{0, 2, 4\}; \newline double Y{[}{]} = \{1, 3, 7\}; \newline \newline int n = sizeof(X)/sizeof(X{[}0{]}); \newline \newline cout \textless{}\textless{} polygonArea(X, Y, n); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Sieve of Eratosthenes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{for (int x = 2; x \textless{}= n; x++) \{ \newline if (sieve{[}x{]}) continue; \newline for (int u = 2*x; u \textless{}= n; u += x) \{ \newline sieve{[}u{]} = x; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{prime Factorization}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//The function divides n by its prime factors, and adds them to the vector. \newline vector\textless{}int\textgreater{} factors(int n) \{ \newline vector\textless{}int\textgreater{} f; \newline for (int x = 2; x*x \textless{}= n; x++) \{ \newline while (n\%x == 0) \{ \newline f.push\_back(x); \newline n /= x; \newline \} \newline \} \newline if (n \textgreater{} 1) f.push\_back(n); \newline return f; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Finding the smallest solution}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = -1; \newline for (int b = z; b \textgreater{}= 1; b /= 2) \{ \newline while (!ok(x+b)) x += b; \newline \} \newline int k = x+1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{An important use for binary search is to find the position where the value of a \newline function changes. Suppose that we wish to find the smallest value k that is a \newline valid solution for a problem. We are given a function ok ( x ) that returns true if x \newline is a valid solution and false otherwise. In addition, we know that ok ( x ) is false \newline when x \textless{} k and true when x ≥ k .} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{vector}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{vector v; \newline v.push\_back(3); // {[}3{]} \newline v.push\_back(2); // {[}3,2{]} \newline v.push\_back(5); // {[}3,2,5{]} \newline \newline cout \textless{}\textless{} v{[}0{]} \textless{}\textless{} "\textbackslash{}n"; // 3 \newline cout \textless{}\textless{} v{[}1{]} \textless{}\textless{} "\textbackslash{}n"; // 2 \newline cout \textless{}\textless{} v{[}2{]} \textless{}\textless{} "\textbackslash{}n"; // 5 \newline \newline for (int i = 0; i \textless{} v.size(); i++) \{ \newline cout \textless{}\textless{} v{[}i{]} \textless{}\textless{} "\textbackslash{}n"; \newline \} \newline \newline for (auto x : v) \{ \newline cout \textless{}\textless{} x \textless{}\textless{} "\textbackslash{}n"; \newline \} \newline \newline sort(v.begin(), v.end()); \newline reverse(v.begin(), v.end()); \newline random\_shuffle(v.begin(), v.end()); \newline //can also be used in the ordinary array like below \newline sort(a, a+n); \newline reverse(a, a+n); \newline random\_shuffle(a, a+n);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Priority queue}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{priority\_queue q; \newline q.push(3); \newline q.push(5); \newline q.push(7); \newline q.push(2); \newline cout \textless{}\textless{} q.top() \textless{}\textless{} "\textbackslash{}n"; // 7 \newline q.pop(); \newline cout \textless{}\textless{} q.top() \textless{}\textless{} "\textbackslash{}n"; // 5 \newline q.pop(); \newline q.push(6); \newline cout \textless{}\textless{} q.top() \textless{}\textless{} "\textbackslash{}n"; // 6 \newline q.pop();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{If we want to create a priority queue that supports finding and removing the smallest element, we can do it as follows: \newline \newline priority\_queue\textless{}int,vector,greater\textgreater{} q;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Stack}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{stack s; \newline s.push(3); \newline s.push(2); \newline s.push(5); \newline cout \textless{}\textless{} s.top(); // 5 \newline s.pop(); \newline cout \textless{}\textless{} s.top(); // 2} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{User-defined structs}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{struct P \{ \newline int x, y; \newline bool operator\textless{}(const P \&p) \{ \newline if (x != p.x) return x \textless{} p.x; \newline else return y \textless{} p.y; \newline \} \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{For example, the following struct P contains the x and y coordinates of a point. \newline The comparison operator is defined so that the points are sorted primarily by the x coordinate and secondarily by the y coordinate.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{BackTracking}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{void search(int y) \{ \newline if (y == n) \{ \newline count++; \newline return; \newline \} \newline for (int x = 0; x \textless{} n; x++) \{ \newline if (column{[}x{]} || diag1{[}x+y{]} || diag2{[}x-y+n-1{]}) continue; \newline column{[}x{]} = diag1{[}x+y{]} = diag2{[}x-y+n-1{]} = 1; \newline search(y+1); \newline column{[}x{]} = diag1{[}x+y{]} = diag2{[}x-y+n-1{]} = 0; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Coin problem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{bool ready{[}N{]}; \newline int value{[}N{]}; \newline \newline int solve(int x) \{ \newline if (x \textless{} 0) return INF; \newline if (x == 0) return 0; \newline if (ready{[}x{]}) return value{[}x{]}; \newline int best = INF; \newline for (auto c : coins) \{ \newline best = min(best, solve(x-c)+1); \newline \newline // Note that we can also iteratively construct the array value using a loop that \newline //simply calculates all the values of solve for parameters 0 . . . n : \newline \newline value{[}0{]} = 0; \newline for (int x = 1; x \textless{}= n; x++) \{ \newline value{[}x{]} = INF; \newline for (auto c : coins) \{ \newline if (x-c \textgreater{}= 0) \{ \newline value{[}x{]} = min(value{[}x{]}, value{[}x-c{]}+1); \newline \} \newline \} \newline \} \newline \} \newline value{[}x{]} = best; \newline ready{[}x{]} = true; \newline return best; \newline \} \newline \newline //constructing the solution \newline int first{[}N{]}; \newline value{[}0{]} = 0; \newline for (int x = 1; x \textless{}= n; x++) \{ \newline value{[}x{]} = INF; \newline for (auto c : coins) \{ \newline if (x-c \textgreater{}= 0 \&\& value{[}x-c{]}+1 \textless{} value{[}x{]}) \{ \newline value{[}x{]} = value{[}x-c{]}+1; \newline first{[}x{]} = c; \newline \} \newline \} \newline \} \newline \newline //to print \newline while (n \textgreater{} 0) \{ \newline cout \textless{}\textless{} first{[}n{]} \textless{}\textless{} "\textbackslash{}n"; \newline n -= first{[}n{]}; \newline \} \newline //Counting the number of solutions \newline //The following code constructs an array count such that count {[} x {]} equals the \newline //value of solve ( x ) for 0 ≤ x ≤ n : \newline count{[}0{]} = 1; \newline for (int x = 1; x \textless{}= n; x++) \{ \newline for (auto c : coins) \{ \newline if (x-c \textgreater{}= 0) \{ \newline count{[}x{]} \%= m; \newline count{[}x{]} += count{[}x-c{]}; \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{where ready {[} x {]} indicates whether the value of solve ( x ) has been calculated, \newline and if it is, value {[} x {]} contains this value. The constant N has been chosen so that \newline all required values fit in the arrays.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Two pointers method (copy)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Subarray sum problem \newline % Row Count 1 (+ 1) s1-The initial subarray contains 3 elements check sum if not contnue to \newline % Row Count 3 (+ 2) s2- The left pointer moves one step to the right. The right pointer does not move check sum if not \newline % Row Count 5 (+ 2) s3- Again, the left pointer moves one step to the right, and this time the right pointer moves three steps to the right check sum if not loop back to s1 \newline % Row Count 9 (+ 4) 2Sum problem \newline % Row Count 10 (+ 1) s1-The initial positions of the pointers are the left pointer at index 0 and the right pointer is at the last index \newline % Row Count 13 (+ 3) s2- Then the left pointer moves one step to the right. The right pointer moves three steps to the left \newline % Row Count 16 (+ 3) s3- After this, the left pointer moves one step to the right again. The right pointer does not move \newline % Row Count 19 (+ 3) s4- loopback to s1% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Two pointers method}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Subarray sum problem \newline % Row Count 1 (+ 1) s1-The initial subarray contains 3 elements check sum if not contnue to \newline % Row Count 3 (+ 2) s2- The left pointer moves one step to the right. The right pointer does not move check sum if not \newline % Row Count 5 (+ 2) s3- Again, the left pointer moves one step to the right, and this time the right pointer moves three steps to the right check sum if not loop back to s1 \newline % Row Count 9 (+ 4) 2Sum problem \newline % Row Count 10 (+ 1) s1-The initial positions of the pointers are the left pointer at index 0 and the right pointer is at the last index \newline % Row Count 13 (+ 3) s2- Then the left pointer moves one step to the right. The right pointer moves three steps to the left \newline % Row Count 16 (+ 3) s3- After this, the left pointer moves one step to the right again. The right pointer does not move \newline % Row Count 19 (+ 3) s4- loopback to s1% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Finding the maximum value}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = -1; \newline for (int b = z; b \textgreater{}= 1; b /= 2) \{ \newline while (f(x+b) \textless{} f(x+b+1)) x += b; \newline \} \newline int k = x+1;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{The idea is to use binary search for finding the largest value of x for which \newline f ( x ) \textless{} f ( x + 1). This implies that k = x + 1 because f ( x + 1) \textgreater{} f ( x + 2).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{multiset}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{multiset s; \newline s.insert(5); \newline s.insert(5); \newline s.insert(5); \newline cout \textless{}\textless{} s.count(5) \textless{}\textless{} "\textbackslash{}n"; // 3 \newline \newline s.erase(5); \newline cout \textless{}\textless{} s.count(5) \textless{}\textless{} "\textbackslash{}n"; // 0 \newline \newline s.erase(s.find(5)); \newline cout \textless{}\textless{} s.count(5) \textless{}\textless{} "\textbackslash{}n"; // 2} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Bitset}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{bitset\textless{}10\textgreater{} s; \newline s{[}1{]} = 1; \newline s{[}3{]} = 1; \newline s{[}4{]} = 1; \newline s{[}7{]} = 1; \newline cout \textless{}\textless{} s{[}4{]} \textless{}\textless{} "\textbackslash{}n"; // 1 \newline cout \textless{}\textless{} s{[}5{]} \textless{}\textless{} "\textbackslash{}n"; // 0 \newline \newline bitset\textless{}10\textgreater{} s(string("0010011010")); // from right to left \newline cout \textless{}\textless{} s{[}4{]} \textless{}\textless{} "\textbackslash{}n"; // 1 \newline cout \textless{}\textless{} s{[}5{]} \textless{}\textless{} "\textbackslash{}n"; // 0 \newline \newline bitset\textless{}10\textgreater{} s(string("0010011010")); \newline cout \textless{}\textless{} s.count() \textless{}\textless{} "\textbackslash{}n"; // 4 num of 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Deque}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{deque d; \newline d.push\_back(5); // {[}5{]} \newline d.push\_back(2); // {[}5,2{]} \newline d.push\_front(3); // {[}3,5,2{]} \newline d.pop\_back(); // {[}3,5{]} \newline d.pop\_front(); // {[}5{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Knapsack problems}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{possible ( x, k ) = possible ( x − w k , k − 1) ∨ possible ( x, k − 1) \newline \newline \newline possible{[}0{]} = true; \newline for (int k = 1; k \textless{}= n; k++) \{ \newline for (int x = W; x \textgreater{}= 0; x-{}-) \{ \newline if (possible{[}x{]}) possible{[}x+w{[}k{]}{]} = true; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{In this section, we focus on the following problem: Given a list of weights \newline {[} w 1 , w 2 , . . . , w n {]}, determine all sums that can be constructed using the weights. \newline For example, if the weights are {[}1, 3, 3, 5{]}, the following sums are possible: all sum between 0 to 12 except 2 and 10} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Paths in a grid}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int sum{[}N{]}{[}N{]}; \newline for (int y = 1; y \textless{}= n; y++) \{ \newline for (int x = 1; x \textless{}= n; x++) \{ \newline sum{[}y{]}{[}x{]} = max(sum{[}y{]}{[}x-1{]},sum{[}y-1{]}{[}x{]})+value{[}y{]}{[}x{]}; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{find a path from the upper-left corner to the lower-right \newline corner of an n × n grid, such that we only move down and right. Each square \newline contains a positive integer, and the path should be constructed so that the sum of \newline the values along the path is as large as possible.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Set iterators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//points to the smallest element in the set \newline auto it = s.begin(); \newline \newline cout \textless{}\textless{} {\emph{it \textless{}\textless{} "\textbackslash{}n"; \newline //print in increasing order \newline for (auto it = s.begin(); it != s.end(); it++) \{ \newline cout \textless{}\textless{} }}it \textless{}\textless{} "\textbackslash{}n"; \newline \} \newline \newline //print the largest element in the set \newline auto it = s.end(); it-{}-; \newline cout \textless{}\textless{} {\emph{it \textless{}\textless{} "\textbackslash{}n"; \newline \newline // find the element x in the set \newline auto it = s.find(x); \newline if (it == s.end()) \{ \newline // x is not found \newline \} \newline \newline \newline //find element nearest to the element x \newline \newline auto it = s.lower\_bound(x); \newline if (it == s.begin()) \{ \newline cout \textless{}\textless{} }}it \textless{}\textless{} "\textbackslash{}n"; \newline \} else if (it == s.end()) \{ \newline it-{}-; \newline cout \textless{}\textless{} {\emph{it \textless{}\textless{} "\textbackslash{}n"; \newline \} else \{ \newline int a = }}it; it-{}-; \newline int b = *it; \newline if (x-b \textless{} a-x) cout \textless{}\textless{} b \textless{}\textless{} "\textbackslash{}n"; \newline else cout \textless{}\textless{} a \textless{}\textless{} "\textbackslash{}n"; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{set implimentation using bit manuplation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = 0; \newline x |= (1\textless{}\textless{}1); \newline x |= (1\textless{}\textless{}3); \newline x |= (1\textless{}\textless{}4); \newline x |= (1\textless{}\textless{}8); \newline cout \textless{}\textless{} \_\_builtin\_popcount(x) \textless{}\textless{} "\textbackslash{}n"; // 4 \newline \newline //Then, the following code prints all elements that belong to the set: \newline for (int i = 0; i \textless{} 32; i++) \{ \newline if (x\&(1\textless{}\textless{}i)) cout \textless{}\textless{} i \textless{}\textless{} " "; \newline \} \newline // output: 1 3 4 8 \newline \newline //For example, the following code first constructs the sets x = \{1, 3, 4, 8\} and \newline //y = \{3, 6, 8, 9\}, and then constructs the set z = x ∪ y = \{1, 3, 4, 6, 8, 9\}: \newline int x = \newline int y = \newline int z = \newline cout \textless{}\textless{} \newline (1\textless{}\textless{}1)|(1\textless{}\textless{}3)|(1\textless{}\textless{}4)|(1\textless{}\textless{}8); \newline (1\textless{}\textless{}3)|(1\textless{}\textless{}6)|(1\textless{}\textless{}8)|(1\textless{}\textless{}9); \newline x|y; \newline \_\_builtin\_popcount(z) \textless{}\textless{} "\textbackslash{}n"; // 6 \newline \newline //The following code goes through the subsets of \{0, 1, . . . , n − 1\}: \newline for (int b = 0; b \textless{} (1\textless{}\textless{}n); b++) \{ \newline // process subset b \newline \} \newline \newline //The following code goes through the subsets of a set x : \newline int b = 0; \newline do \{ \newline // process subset b \newline \} while (b=(b-x)\&x); \newline \newline //Hamming distance \newline int hamming(int a, int b) \{ \newline return \_\_builtin\_popcount(a\textasciicircum{}b); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Bitmanuplation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = \newline cout \textless{}\textless{} \newline cout \textless{}\textless{} \newline cout \textless{}\textless{} \newline cout \textless{}\textless{} \newline 5328; // \seqsplit{00000000000000000001010011010000} \newline \_\_builtin\_clz(x) \textless{}\textless{} "\textbackslash{}n"; // 19 \newline \_\_builtin\_ctz(x) \textless{}\textless{} "\textbackslash{}n"; // 4 \newline \_\_builtin\_popcount(x) \textless{}\textless{} "\textbackslash{}n"; // 5 \newline \_\_builtin\_parity(x) \textless{}\textless{} "\textbackslash{}n"; // 1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}