-
Notifications
You must be signed in to change notification settings - Fork 115
/
comprog.tex
1301 lines (1139 loc) · 56 KB
/
comprog.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[8pt,a4paper,landscape,oneside]{amsart}
\usepackage{amsmath, amsthm, amssymb, amsfonts}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{float}
\usepackage{fullpage}
%\usepackage{geometry}
% \usepackage[top=0pt, bottom=1cm, left=0.3cm, right=0.3cm]{geometry}
\usepackage[top=3pt, bottom=1cm, left=0.3cm, right=0.3cm]{geometry}
\usepackage{graphicx}
% \usepackage{listings}
\usepackage{subcaption}
\usepackage[scaled]{beramono}
\usepackage{titling}
\usepackage{datetime}
\usepackage{enumitem}
\usepackage{multicol}
\newcommand{\subtitle}[1]{%
\posttitle{%
\par\end{center}
\begin{center}\large#1\end{center}
\vskip0.1em\vspace{-1em}}%
}
% Minted
\usepackage{minted}
\newcommand{\code}[1]{\inputminted[fontsize=\normalsize,baselinestretch=1]{cpp}{_code/#1}}
\newcommand{\bashcode}[1]{\inputminted{bash}{_code/#1}}
\newcommand{\regcode}[1]{\inputminted{cpp}{code/#1}}
% Header/Footer
% \geometry{includeheadfoot}
%\fancyhf{}
\pagestyle{fancy}
\lhead{Reykjavík University}
\rhead{\thepage}
\cfoot{}
\setlength{\headheight}{15.2pt}
\setlength{\droptitle}{-20pt}
\posttitle{\par\end{center}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
% Math and bit operators
\DeclareMathOperator{\lcm}{lcm}
\newcommand*\BitAnd{\mathrel{\&}}
\newcommand*\BitOr{\mathrel{|}}
\newcommand*\ShiftLeft{\ll}
\newcommand*\ShiftRight{\gg}
\newcommand*\BitNeg{\ensuremath{\mathord{\sim}}}
\DeclareRobustCommand{\stirling}{\genfrac\{\}{0pt}{}}
\newenvironment{myitemize}
{ \begin{itemize}[leftmargin=.5cm]
\setlength{\itemsep}{0pt}
\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt} }
{ \end{itemize} }
% Title/Author
\title{.*RU.*}
\subtitle{Team Reference Document}
\date{\ddmmyyyydate{\today{}}}
% Output Verbosity
\newif\ifverbose
\verbosetrue
% \verbosefalse
\begin{document}
\begin{multicols*}{3}
\maketitle
\thispagestyle{fancy}
\vspace{-3em}
% \addtocontents{toc}{\protect\enlargethispage{\baselineskip}}
\tableofcontents
% \clearpage
\section{Code Templates}
\subsection{Basic Configuration}
\subsubsection{.bashrc}
\bashcode{bashrc.sh}
ProTip\texttrademark: setxkbmap dvorak on qwerty: \texttt{o.yqtxmal ekrpat}
\subsubsection{.vimrc}
\bashcode{vimrc.sh}
\subsection{C++ Header}
A C++ header.
\code{header.cpp}
\subsection{Java Template}
A Java template.
\code{template.java}
\section{Data Structures}
\subsection{Union-Find}
\ifverbose
An implementation of the Union-Find disjoint sets data structure.
\fi
\code{data-structures/union_find.cpp}
\subsection{Segment Tree}
\ifverbose
An implementation of a Segment Tree.
\fi
\code{data-structures/segment_tree_node.cpp}
\ifverbose
\code{data-structures/segment_tree_mnnode.cpp}
\fi
\code{data-structures/segment_tree.cpp}
\subsubsection{Persistent Segment Tree}
\code{data-structures/persistent_segment_tree.cpp}
\subsection{Fenwick Tree}
\ifverbose
A Fenwick Tree is a data structure that represents an array of $n$
numbers. It supports adjusting the $i$-th element in $O(\log n)$ time,
and computing the sum of numbers in the range $i..j$ in $O(\log n)$
time. It only needs $O(n)$ space.
\fi
\code{data-structures/fenwick_tree.cpp}
\subsection{Matrix}
\ifverbose
A Matrix class.
\fi
\code{data-structures/matrix.cpp}
\ifverbose
\subsection{AVL Tree}
\ifverbose
A fast, easily augmentable, balanced binary search tree.
\fi
\code{data-structures/avl_tree.cpp}
\ifverbose
Also a very simple wrapper over the AVL tree that implements a map
interface.
\code{data-structures/avl_map.cpp}
\fi
\fi
\subsection{Cartesian Tree}
\code{data-structures/cartesian_tree.cpp}
\ifverbose
\subsection{Heap}
An implementation of a binary heap.
\code{data-structures/heap.cpp}
\fi
\ifverbose
\subsection{Dancing Links}
\ifverbose
An implementation of Donald Knuth's Dancing Links data structure. A
linked list supporting deletion and restoration of elements.
\fi
\code{data-structures/dancing_links.cpp}
\fi
\subsection{Misof Tree}
\ifverbose
A simple tree data structure for inserting, erasing, and querying the
$n$th largest element.
\fi
\code{data-structures/misof_tree.cpp}
\ifverbose
\subsection{$k$-d Tree}
\ifverbose
A $k$-dimensional tree supporting fast construction, adding points, and
nearest neighbor queries.
\fi
\code{data-structures/kd_tree.cpp}
\fi
\subsection{Sqrt Decomposition}
\ifverbose
Design principle that supports many operations in amortized $\sqrt{n}$ per operation.
\fi
\code{data-structures/sqrt_decomposition.cpp}
\subsection{Monotonic Queue}
\ifverbose
A queue that supports querying for the minimum element. Useful for sliding window algorithms.
\fi
\code{data-structures/monotonic_queue.cpp}
\subsection{Convex Hull Trick}
If converting to integers, look out for division by 0 and $\pm\infty$.
\code{data-structures/convex_hull_trick.cpp}
And dynamic variant:
\code{data-structures/convex_hull_trick_dynamic.cpp}
\subsection{Sparse Table}
\code{data-structures/sparse_table.cpp}
\section{Graphs}
\subsection{Single-Source Shortest Paths}
\subsubsection{Dijkstra's algorithm}
\ifverbose
An implementation of Dijkstra's algorithm. It runs in
$\Theta(|E|\log{|V|})$ time.
\fi
\code{graph/dijkstra.cpp}
\subsubsection{Bellman-Ford algorithm}
\ifverbose
The Bellman-Ford algorithm solves the single-source shortest paths
problem in $O(|V||E|)$ time. It is slower than Dijkstra's
algorithm, but it works on graphs with negative edges and has the
ability to detect negative cycles, neither of which Dijkstra's
algorithm can do.
\fi
\code{graph/bellman_ford.cpp}
\subsubsection{IDA$^\star$ algorithm}
\code{graph/idastar.cpp}
\ifverbose
\subsection{All-Pairs Shortest Paths}
\subsubsection{Floyd-Warshall algorithm}
The Floyd-Warshall algorithm solves the all-pairs shortest paths
problem in $O(|V|^3)$ time.
\code{graph/floyd_warshall.cpp}
\fi
\subsection{Strongly Connected Components}
\subsubsection{Kosaraju's algorithm}
\ifverbose
Kosarajus's algorithm finds strongly connected components of a
directed graph in $O(|V|+|E|)$ time.
\fi
Returns a Union-Find of the SCCs, as well as a topological ordering
of the SCCs. Note that the ordering specifies a random element from
each SCC, not the UF parents!
\code{graph/scc.cpp}
\subsection{Cut Points and Bridges}
\code{graph/cut_points_and_bridges.cpp}
\ifverbose
\subsection{Minimum Spanning Tree}
\subsubsection{Kruskal's algorithm}
\code{graph/kruskals_mst.cpp}
\fi
\ifverbose
\subsection{Topological Sort}
\subsubsection{Modified Depth-First Search}
\code{graph/tsort.cpp}
\fi
\subsection{Euler Path}
\ifverbose
Finds an euler path (or circuit) in a directed graph, or reports that
none exist.
\fi
\code{graph/euler_path.cpp}
And an undirected version, which finds a cycle.
\code{graph/euler_path_undirected.cpp}
\subsection{Bipartite Matching}
\subsubsection{Alternating Paths algorithm}
\ifverbose
The alternating paths algorithm solves bipartite matching in $O(mn^2)$
time, where $m$, $n$ are the number of vertices on the left and right
side of the bipartite graph, respectively.
\fi
\code{graph/bipartite_matching.cpp}
\subsubsection{Hopcroft-Karp algorithm}
\ifverbose
An implementation of Hopcroft-Karp algorithm for bipartite
matching.
\fi
Running time is $O(|E|\sqrt{|V|})$.
\code{graph/hopcroft_karp.cpp}
\subsubsection{Minimum Vertex Cover in Bipartite Graphs}
\code{graph/bipartite_mvc.cpp}
\subsection{Maximum Flow}
\subsubsection{Dinic's algorithm}
An implementation of Dinic's algorithm that runs in
$O(|V|^2|E|)$.
\ifverbose
It computes the maximum flow of a flow network.
\fi
\code{graph/dinic.cpp}
\ifverbose
\subsubsection{Edmonds Karp's algorithm}
An implementation of Edmonds Karp's algorithm that runs in
$O(|V||E|^2)$.
\ifverbose
It computes the maximum flow of a flow network.
\fi
\code{graph/edmonds_karps.cpp}
\fi
\subsection{Minimum Cost Maximum Flow}
\ifverbose
An implementation of Edmonds Karp's algorithm, modified to find
shortest path to augment each time (instead of just any path). It
computes the maximum flow of a flow network, and when there are
multiple maximum flows, finds the maximum flow with minimum cost.
\fi
Running time is $O(|V|^2|E|\log|V|)$.
\code{graph/edmonds_karps_mcmf.cpp}
\subsection{All Pairs Maximum Flow}
\subsubsection{Gomory-Hu Tree}
An implementation of the Gomory-Hu Tree. The spanning tree is constructed using Gusfield's algorithm
in $O(|V| ^ 2)$ plus $|V|-1$ times the time it takes to calculate the maximum flow.
If Dinic's algorithm is used to calculate the max flow, the running time is $O(|V|^3|E|)$.
NOTE: Not sure if it works correctly with disconnected graphs.
\code{graph/gomory_hu_tree.cpp}
\subsection{Heavy-Light Decomposition}
\code{graph/hld.cpp}
\subsection{Centroid Decomposition}
\code{graph/centroid_decomposition.cpp}
\subsection{Least Common Ancestors, Binary Jumping}
\code{graph/lca.cpp}
\subsection{Tarjan's Off-line Lowest Common Ancestors Algorithm}
\code{graph/tarjan_olca.cpp}
\subsection{Minimum Mean Weight Cycle}
Given a strongly connected directed graph, finds the cycle of minimum
mean weight. If you have a graph that is not strongly connected, run
this on each strongly connected component.
\code{graph/min_mean_cycle.cpp}
\subsection{Minimum Arborescence}
Given a weighted directed graph, finds a subset of edges of minimum
total weight so that there is a unique path from the root $r$ to each
vertex. Returns a vector of size $n$, where the $i$th element is the
edge for the $i$th vertex. The answer for the root is undefined!
\code{graph/arborescence.cpp}
\subsection{Blossom algorithm}
Finds a maximum matching in an arbitrary graph in $O(|V|^4)$ time. Be
vary of loop edges.
\code{graph/blossom.cpp}
\subsection{Maximum Density Subgraph}
Given (weighted) undirected graph $G$. Binary search density. If $g$ is
current density, construct flow network: $(S, u, m)$, $(u, T,
m+2g-d_u)$, $(u,v,1)$, where $m$ is a large constant (larger than sum
of edge weights). Run floating-point max-flow. If minimum cut has empty
$S$-component, then maximum density is smaller than $g$, otherwise it's
larger. Distance between valid densities is at least $1/(n(n-1))$. Edge
case when density is $0$. This also works for weighted graphs by
replacing $d_u$ by the weighted degree, and doing more iterations (if
weights are not integers).
\subsection{Maximum-Weight Closure}
Given a vertex-weighted directed graph $G$. Turn the graph into a flow
network, adding weight $\infty$ to each edge. Add vertices $S,T$. For
each vertex $v$ of weight $w$, add edge $(S,v,w)$ if $w\geq 0$, or edge
$(v,T,-w)$ if $w<0$. Sum of positive weights minus minimum $S-T$ cut is
the answer. Vertices reachable from $S$ are in the closure. The
maximum-weight closure is the same as the complement of the
minimum-weight closure on the graph with edges reversed.
\subsection{Maximum Weighted Independent Set in a Bipartite Graph}
This is the same as the minimum weighted vertex cover. Solve this by
constructing a flow network with edges $(S,u,w(u))$ for $u\in L$,
$(v,T,w(v))$ for $v\in R$ and $(u,v,\infty)$ for $(u,v)\in E$. The
minimum $S,T$-cut is the answer. Vertices adjacent to a cut edge are
in the vertex cover.
\subsection{Synchronizing word problem}
A DFA has a synchronizing word (an input sequence that moves all states
to the same state) iff.\ each pair of states has a synchronizing word.
That can be checked using reverse DFS over pairs of states. Finding the
shortest synchronizing word is NP-complete.
\subsection{Max flow with lower bounds on edges}
% TODO: Test this!
Change edge $(u,v,l\leq f\leq c)$ to $(u,v,f\leq c-l)$. Add edge
$(t,s,\infty)$. Create super-nodes $S$, $T$. Let $M(u) = \sum_{v}
l(v,u) - \sum_{v} l(u,v)$. If $M(u)<0$, add edge $(u,T,-M(u))$, else
add edge $(S,u,M(u))$. Max flow from $S$ to $T$. If all edges from $S$
are saturated, then we have a feasible flow. Continue running max flow
from $s$ to $t$ in original graph.
% TODO: Was there something similar for vertex capacities that we should add?
\subsection{Tutte matrix for general matching}
Create an $n\times n$ matrix $A$. For each edge $(i,j)$, $i<j$, let
$A_{ij} = x_{ij}$ and $A_{ji} = -x_{ij}$. All other entries are $0$.
The determinant of $A$ is zero iff.\ the graph has a perfect matching.
A randomized algorithm uses the Schwartz--Zippel lemma to check if it is
zero.
\section{Strings}
\subsection{The Knuth-Morris-Pratt algorithm}
\ifverbose
An implementation of the Knuth-Morris-Pratt algorithm. Runs in $O(n+m)$
time, where $n$ and $m$ are the lengths of the string and the pattern.
\fi
\code{strings/kmp.cpp}
\subsection{The $Z$ algorithm}
\ifverbose
Given a string $S$, $Z_i(S)$ is the longest substring of $S$ starting
at $i$ that is also a prefix of $S$. The $Z$ algorithm computes these
$Z$ values in $O(n)$ time, where $n = |S|$. $Z$ values can, for
example, be used to find all occurrences of a pattern $P$ in a string
$T$ in linear time. This is accomplished by computing $Z$ values of $S
= P T$, and looking for all $i$ such that $Z_i \geq |P|$.
\fi
\code{strings/z_algorithm.cpp}
\ifverbose
\subsection{Trie}
A Trie class.
\code{strings/trie.cpp}
\fi
\subsection{Suffix Array}
An $O(n \log^2 n)$ construction of a Suffix Tree.
\code{strings/suffix_array.cpp}
\subsection{Aho-Corasick Algorithm}
\ifverbose
An implementation of the Aho-Corasick algorithm. Constructs a state
machine from a set of keywords which can be used to search a string for
any of the keywords.
\fi
\code{strings/aho_corasick.cpp}
\subsection{eerTree}
\ifverbose
Constructs an eerTree in $O(n)$, one character at a time.
\fi
\code{strings/eertree.cpp}
% http://arxiv.org/pdf/1506.04862v1.pdf
\subsection{Suffix Automaton}
Minimum automata that accepts all suffixes of a string with $O(n)$
construction. The automata itself is a DAG therefore suitable for DP,
examples are counting unique substrings, occurrences of substrings and
suffix.
\code{strings/suffix_automaton.cpp}
\subsection{Hashing}
Modulus should be a large prime. Can also use multiple instances with
different moduli to minimize chance of collision.
\code{strings/hasher.cpp}
\section{Mathematics}
\ifverbose
\subsection{Fraction}
A fraction (rational number) class. Note that numbers are stored in
lowest common terms.
\code{mathematics/fraction.cpp}
\fi
\ifverbose
\subsection{Big Integer}
\ifverbose
A big integer class.
\fi
\code{mathematics/intx.cpp}
\subsubsection{Fast Multiplication}
\ifverbose
Fast multiplication for the big integer using Fast Fourier Transform.
\fi
\code{mathematics/fastmul.cpp}
\fi
\subsection{Binomial Coefficients}
The binomial coefficient $\binom{n}{k} = \frac{n!}{k!(n-k)!}$ is the
number of ways to choose $k$ items out of a total of $n$ items. Also
contains an implementation of Lucas' theorem for computing the answer
modulo a prime $p$. Use modular multiplicative inverse if needed, and
be very careful of overflows.
\code{mathematics/nck.cpp}
\subsection{Euclidean algorithm}
\ifverbose
The Euclidean algorithm computes the greatest common divisor of two
integers $a$, $b$.
\fi
\code{mathematics/gcd.cpp}
The extended Euclidean algorithm computes the greatest common divisor
$d$ of two integers $a$, $b$ and also finds two integers $x$, $y$ such
that $a\times x + b\times y = d$.
\code{mathematics/egcd.cpp}
\subsection{Trial Division Primality Testing}
\ifverbose
An optimized trial division to check whether an integer is prime.
\fi
\code{mathematics/is_prime.cpp}
\subsection{Miller-Rabin Primality Test}
\ifverbose
The Miller-Rabin probabilistic primality test.
\fi
\code{mathematics/miller_rabin.cpp}
\subsection{Pollard's $\rho$ algorithm}
\code{mathematics/pollard_rho.cpp}
\subsection{Sieve of Eratosthenes}
\ifverbose
An optimized implementation of Eratosthenes' Sieve.
\fi
\code{mathematics/prime_sieve.cpp}
\subsection{Divisor Sieve}
\ifverbose
A O(n) prime sieve. Computes the smallest divisor of any number up to n.
\fi
\code{mathematics/divisor_sieve.cpp}
\ifverbose
\subsection{Modular Exponentiation}
A function to perform fast modular exponentiation.
\code{mathematics/mod_pow.cpp}
\fi
\subsection{Modular Multiplicative Inverse}
\ifverbose
A function to find a modular multiplicative inverse. Alternatively use
\texttt{mod\_{}pow(a,m-2,m)} when $m$ is prime.
\fi
\code{mathematics/mod_inv.cpp}
\ifverbose
A sieve version:
\fi
\code{mathematics/mod_inv_sieve.cpp}
\subsection{Primitive Root}
\code{mathematics/primitive_root.cpp}
\subsection{Chinese Remainder Theorem}
\ifverbose
An implementation of the Chinese Remainder Theorem.
\fi
\code{mathematics/crt.cpp}
\subsection{Linear Congruence Solver}
Given $ax \equiv b \pmod{n}$, returns $(t,m)$ such that all solutions
are given by $x\equiv t\pmod{m}$. No solutions iff $(0,0)$ is returned.
\code{mathematics/linear_congruence.cpp}
\subsection{Berlekamp-Massey algorithm}
Given a sequence of integers in some field, finds a linear recurrence
of minimum order that generates the sequence in $O(n^2)$.
\code{mathematics/berlekamp_massey.cpp}
\subsection{Tonelli-Shanks algorithm}
Given prime $p$ and integer $1\leq n<p$, returns the square root $r$ of
$n$ modulo $p$. There is also another solution given by $-r$ modulo
$p$.
\code{mathematics/tonelli_shanks.cpp}
\subsection{Numeric Integration}
\ifverbose
Numeric integration using Simpson's rule.
\fi
\code{mathematics/numeric_integration.cpp}
\subsection{Linear Recurrence Relation}
Computes the $n$-th term satisfying the linear recurrence relation with
initial terms \texttt{init} and coefficients \texttt{c} in $O(k^2\log n)$.
\code{mathematics/linear_recurrence.cpp}
\subsection{Fast Fourier Transform}
The Cooley-Tukey algorithm for quickly computing the discrete Fourier
transform. The \texttt{fft} function only supports powers of twos. The
\texttt{czt} function implements the Chirp Z-transform and supports any
size, but is slightly slower.
\code{mathematics/fft.cpp}
\subsection{Number-Theoretic Transform}
Other possible moduli: $2\,113\,929\,217$ ($2^{25}$), $2\,013\,265\,920\,268\,435\,457$ ($2^{28}$, with $g=5$). % XXX: Should $g=1976010382590097340$, or is $g=5$ alright as well?
\code{mathematics/ntt.cpp}
\subsection{Fast Hadamard Transform}
Computes the Hadamard transform of the given array. Can be used to
compute the \texttt{XOR}-convolution of arrays, exactly like with FFT.
For \texttt{AND}-convolution, use $(x+y,y)$ and $(x-y,y)$. For
\texttt{OR}-convolution, use $(x,x+y)$ and $(x,-x+y)$. \textbf{Note}:
Size of array must be a power of $2$.
\code{mathematics/fht.cpp}
\subsection{Tridiagonal Matrix Algorithm}
Solves a tridiagonal system of linear equations $a_ix_{i-1} + b_ix_i +
c_ix_{i+1} = d_i$ where $a_1 = c_n = 0$. Beware of numerical
instability.
\code{mathematics/tridiagonal.cpp}
\subsection{Mertens Function}
Mertens function is $M(n) = \sum_{i=1}^n \mu(i)$. Let $L\approx
(n\log{\log{n}})^{2/3}$ and the algorithm runs in $O(n^{2/3})$.
\ifverbose
\else
Can also be easily changed to compute the summatory $\Phi$.
\fi
\code{mathematics/mertens.cpp}
\ifverbose
\subsection{Summatory Phi}
The summatory phi function $\Phi(n) = \sum_{i=1}^n \phi(i)$. Let $L\approx
(n\log{\log{n}})^{2/3}$ and the algorithm runs in $O(n^{2/3})$.
\code{mathematics/summatory_phi.cpp}
\fi
\subsection{Prime $\pi$}
Returns $\pi\left(\lfloor n/k\rfloor\right)$ for all $1\leq k \leq n$,
where $\pi(n)$ is the number of primes $\leq n$. Can also be modified
to accumulate any multiplicative function over the primes.
\code{mathematics/primepi.cpp}
\subsection{Josephus problem}
Last man standing out of $n$ if every $k$th is killed. Zero-based, and
does not kill $0$ on first pass.
\code{mathematics/josephus.cpp}
\subsection{Number of Integer Points under Line}
Count the number of integer solutions to $Ax+By\leq C$, $0 \leq x \leq
n$, $0 \leq y$. In other words, evaluate the sum $\sum_{x=0}^n
\left\lfloor \frac{C-Ax}{B} + 1\right\rfloor$. To count all solutions,
let $n = \left\lfloor \frac{c}{a}\right\rfloor$. In any case, it must hold
that $C-nA \geq 0$. Be very careful about overflows.
\code{mathematics/floor_sum.cpp}
\subsection{Numbers and Sequences}
Some random prime numbers: 1031, 32771, 1048583, 33554467,
1073741827, 34359738421, 1099511627791, 35184372088891,
1125899906842679, 36028797018963971.
More random prime numbers: $10^3 + \{-9,-3,9,13\}$, $10^6+
\{-17,3,33\}$, $10^9+ \{7,9,21,33,87\}$.
Some maximal divisor counts:
\begin{tabular}{rr}
840 & 32 \\
720\,720 & 240 \\
735\,134\,400 & 1\,344 \\
963\,761\,198\,400 & 6\,720 \\
866\,421\,317\,361\,600 & 26\,880 \\
897\,612\,484\,786\,617\,600 & 103\,680 \\
\end{tabular}
\subsection{Game Theory}
\begin{itemize}
\item Useful identity: $\oplus_{x=0}^{a-1}\, x = [0,a-1,1,a][a\% 4]$
\item \textbf{Nim}: Winning position if $n_1\oplus \cdots \oplus n_k = 0$
\item \textbf{Misére Nim}: Winning position if some $n_i > 1$ and $n_1\oplus \cdots \oplus n_k = 0$, or all $n_i \leq 1$ and $n_1\oplus \cdots \oplus n_k = 1$
\end{itemize}
\section{Geometry}
\subsection{Primitives}
\ifverbose
Geometry primitives.
\fi
\code{geometry/primitives.cpp}
\subsection{Lines}
\ifverbose
Line related functions.
\fi
\code{geometry/lines.cpp}
\subsection{Circles}
\ifverbose
Circle related functions.
\fi
\code{geometry/circles.cpp}
\subsection{Polygon}
\ifverbose
Polygon primitives.
\fi
\code{geometry/polygon.cpp}
\subsection{Convex Hull}
\ifverbose
An algorithm that finds the Convex Hull of a set of points.
\fi
NOTE: Doesn't work on some weird edge cases. (A small case that
included three collinear lines would return the same point on both the
upper and lower hull.)
\code{geometry/convex_hull.cpp}
\subsection{Line Segment Intersection}
\ifverbose
Computes the intersection between two line segments.
\fi
\code{geometry/line_segment_intersect.cpp}
\subsection{Great-Circle Distance}
Computes the distance between two points (given as latitude/longitude
coordinates) on a sphere of radius $r$.
\code{geometry/gc_distance.cpp}
\subsection{Smallest Enclosing Circle}
Computes the smallest enclosing circle using Welzl's algorithm in
expected $O(n)$ time.
\code{geometry/welzl.cpp}
\subsection{Closest Pair of Points}
\ifverbose
A sweep line algorithm for computing the distance between the closest
pair of points.
\fi
\code{geometry/closest_pair.cpp}
\subsection{3D Primitives}
\ifverbose
Three-dimensional geometry primitives.
\fi
\code{geometry/primitives3d.cpp}
\subsection{3D Convex Hull}
\code{geometry/convex_hull3d.cpp}
\subsection{Polygon Centroid}
\code{geometry/polygon_centroid.cpp}
\subsection{Rotating Calipers}
\code{geometry/rotating_calipers.cpp}
\subsection{Rectilinear Minimum Spanning Tree}
Given a set of $n$ points in the plane, and the aim is to find a
minimum spanning tree connecting these $n$ points, assuming the
Manhattan distance is used. The function \texttt{candidates} returns at
most $4n$ edges that are a superset of the edges in a minimum spanning
tree, and then one can use Kruskal's algorithm.
\code{geometry/rmst.cpp}
\subsection{Line upper/lower envelope}
To find the upper/lower envelope of a collection of lines $a_i+b_i x$,
plot the points $(b_i,a_i)$, add the point $(0,\pm \infty)$ (depending
on if upper/lower envelope is desired), and then find the convex hull.
\subsection{Formulas}
Let $a = (a_x, a_y)$ and $b = (b_x, b_y)$ be two-dimensional vectors.
\begin{itemize}
\item $a\cdot b = |a||b|\cos{\theta}$, where $\theta$ is the angle
between $a$ and $b$.
\item $a\times b = |a||b|\sin{\theta}$, where $\theta$ is the
signed angle between $a$ and $b$.
\item $a\times b$ is equal to the area of the parallelogram with
two of its sides formed by $a$ and $b$. Half of that is the
area of the triangle formed by $a$ and $b$.
\item The line going through $a$ and $b$ is $Ax+By=C$ where $A=b_y-a_y$, $B=a_x-b_x$, $C=Aa_x+Ba_y$.
\item Two lines $A_1x+B_1y=C_1$, $A_2x+B_2y=C_2$ are parallel iff.\ $D=A_1B_2-A_2B_1$ is zero. Otherwise their unique intersection is $(B_2C_1-B_1C_2,A_1C_2-A_2C_1)/D$.
\item \textbf{Euler's formula:} $V - E + F = 2$
\item Side lengths $a,b,c$ can form a triangle iff.\ $a+b>c$, $b+c>a$ and $a+c>b$.
\item Sum of internal angles of a regular convex $n$-gon is $(n-2)\pi$.
\item \textbf{Law of sines:} $\frac{a}{\sin A} = \frac{b}{\sin B} = \frac{c}{\sin C}$
\item \textbf{Law of cosines:} $b^2 = a^2 + c^2 - 2ac\cos B$
\item Internal tangents of circles $(c_1,r_1), (c_2,r_2)$ intersect at $(c_1r_2+c_2r_1)/(r_1+r_2)$, external intersect at $(c_1r_2-c_2r_1)/(r_1+r_2)$.
\end{itemize}
\section{Other Algorithms}
\subsection{2SAT}
\ifverbose
A fast 2SAT solver.
\fi
\code{other/two_sat.cpp}
\subsection{DPLL Algorithm}
A SAT solver that can solve a random 1000-variable SAT instance within a second.
\code{other/dpll.cpp}
\subsection{Stable Marriage}
\ifverbose
The Gale-Shapley algorithm for solving the stable marriage problem.
\fi
\code{other/stable_marriage.cpp}
\subsection{Algorithm X}
\ifverbose
An implementation of Knuth's Algorithm X, using dancing links. Solves the Exact Cover problem.
\fi
\code{other/algorithm_x.cpp}
\subsection{Matroid Intersection}
Computes the maximum weight and cardinality intersection of two
matroids, specified by implementing the required abstract methods, in
$O(n^3(M_1+M_2))$.
\code{other/matroid_intersection.cpp}
\subsection{$n$th Permutation}
\ifverbose
A very fast algorithm for computing the $n$th permutation of the list
$\{0,1,\ldots,k-1\}$.
\fi
\code{other/nth_permutation.cpp}
\subsection{Cycle-Finding}
\ifverbose
An implementation of Floyd's Cycle-Finding algorithm.
\fi
\code{other/floyds_algorithm.cpp}
\subsection{Longest Increasing Subsequence}
\code{other/lis.cpp}
\subsection{Dates}
\ifverbose
Functions to simplify date calculations.
\fi
\code{other/dates.cpp}
\subsection{Simulated Annealing}
An example use of Simulated Annealing to find a permutation of length $n$
that maximizes $\sum_{i=1}^{n-1}|p_i - p_{i+1}|$.
\code{other/simulated_annealing.cpp}
\subsection{Simplex}
\code{other/simplex.cpp}
\subsection{Fast Square Testing}
An optimized test for square integers.
\code{tricks/is_square.cpp}
\subsection{Fast Input Reading}
\ifverbose
If input or output is huge, sometimes it is beneficial to optimize the
input reading/output writing. This can be achieved by reading all input
in at once (using fread), and then parsing it manually. Output can also
be stored in an output buffer and then dumped once in the end (using
fwrite). A simpler, but still effective, way to achieve speed is to use
the following input reading method.
\fi
\code{tricks/fast_input.cpp}
\ifverbose
\subsection{128-bit Integer}
GCC has a 128-bit integer data type named \texttt{\_\_int128}. Useful
if doing multiplication of 64-bit integers, or something needing a
little more than 64-bits to represent. There's also
\texttt{\_\_float128}.
\fi
\subsection{Bit Hacks}
\code{tricks/snoob.cpp}
\newpage
\begin{tabular}{@{}l|l|l@{}}
\toprule
Catalan & $C_0=1$, $C_n=\frac{1}{n+1}\binom{2n}{n} = \sum_{i=0}^{n-1}C_iC_{n-i-1} = \frac{4n-2}{n+1}C_{n-1}$ & \\
Stirling 1st kind & $\left[{0\atop 0}\right]=1$, $\left[{n\atop 0}\right]=\left[{0\atop n}\right]=0$, $\left[{n\atop k}\right]=(n-1)\left[{n-1\atop k}\right]+\left[{n-1\atop k-1}\right]$ & \#perms of $n$ objs with exactly $k$ cycles\\
Stirling 2nd kind & $\left\{{n\atop 1}\right\}=\left\{{n\atop n}\right\}=1$, $\left\{{n\atop k}\right\} = k \left\{{ n-1 \atop k }\right\} + \left\{{n-1\atop k-1}\right\}$ & \#ways to partition $n$ objs into $k$ nonempty sets\\
Euler & $\left \langle {n\atop 0} \right \rangle = \left \langle {n\atop n-1} \right \rangle = 1 $, $\left \langle {n\atop k} \right \rangle = (k+1) \left \langle {n-1\atop {k}} \right \rangle + (n-k)\left \langle {{n-1}\atop {k-1}} \right \rangle$ & \#perms of $n$ objs with exactly $k$ ascents \\
Euler 2nd Order & $\left \langle \!\!\left \langle {n\atop k} \right \rangle \!\! \right \rangle = (k+1) \left \langle \!\! \left \langle {{n-1}\atop {k}} \right \rangle \!\! \right \rangle +(2n-k-1)\left \langle \!\! \left \langle {{n-1}\atop {k-1}} \right \rangle \!\! \right \rangle$ & \#perms of ${1,1,2,2,...,n,n}$ with exactly $k$ ascents \\
Bell & $B_1 = 1$, $B_n = \sum_{k=0}^{n-1} B_k \binom{n-1}{k} = \sum_{k=0}^n\left\{{n\atop k}\right\}$ & \#partitions of $1..n$ (Stirling 2nd, no limit on k)\\
\bottomrule
\end{tabular}
\vspace{10pt}
\begin{tabular}{ll}
\#labeled rooted trees & $n^{n-1}$ \\
\#labeled unrooted trees & $n^{n-2}$ \\
\#forests of $k$ rooted trees & $\frac{k}{n}\binom{n}{k}n^{n-k}$ \\
% Kirchoff's theorem
$\sum_{i=1}^n i^2 = n(n+1)(2n+1)/6$ & $\sum_{i=1}^n i^3 = n^2(n+1)^2/4$ \\
$!n = n\times!(n-1)+(-1)^n$ & $!n = (n-1)(!(n-1)+!(n-2))$ \\
$\sum_{i=1}^n \binom{n}{i} F_i = F_{2n}$ & $\sum_{i} \binom{n-i}{i} = F_{n+1}$ \\
$\sum_{k=0}^n \binom{k}{m} = \binom{n+1}{m+1}$ & $x^k = \sum_{i=0}^k i!\stirling{k}{i}\binom{x}{i} = \sum_{i=0}^k \left\langle {k \atop i} \right\rangle\binom{x+i}{k}$ \\
$a\equiv b\pmod{x,y} \Rightarrow a\equiv b\pmod{\lcm(x,y)}$ & $\sum_{d|n} \phi(d) = n$ \\
$ac\equiv bc\pmod{m} \Rightarrow a\equiv b\pmod{\frac{m}{\gcd(c,m)}}$ & $(\sum_{d|n} \sigma_0(d))^2 = \sum_{d|n} \sigma_0(d)^3$ \\
$p$ prime $\Leftrightarrow (p-1)!\equiv -1\pmod{p}$ & $\gcd(n^a-1,n^b-1) = n^{\gcd(a,b)}-1$ \\
$\sigma_x(n) = \prod_{i=0}^{r} \frac{p_i^{(a_i + 1)x} - 1}{p_i^x - 1}$ & $\sigma_0(n) = \prod_{i=0}^r (a_i + 1)$ \\
$\sum_{k=0}^m (-1)^k \binom{n}{k} = (-1)^m \binom{n-1}{m}$ & \\
$2^{\omega(n)} = O(\sqrt{n})$ & $\sum_{i=1}^n 2^{\omega(i)} = O(n \log n)$ \\
% Kinematic equations
$d = v_i t + \frac{1}{2}at^2$ & $v_f^2 = v_i^2 + 2ad$ \\
$v_f = v_i + at$ & $d = \frac{v_i + v_f}{2}t$ \\
\end{tabular}
\subsection{The Twelvefold Way}
Putting $n$ balls into $k$ boxes.\\
\begin{tabular}{@{}c|c|c|c|c|l@{}}
Balls & same & distinct & same & distinct & \\
Boxes & same & same & distinct & distinct & Remarks\\
\hline
- & $\mathrm{p}_k(n)$ & $\sum_{i=0}^k \stirling{n}{i}$ & $\binom{n+k-1}{k-1}$ & $k^n$ & $\mathrm{p}_k(n)$: \#partitions of $n$ into $\le k$ positive parts \\
$\mathrm{size}\ge 1$ & $\mathrm{p}(n,k)$ & $\stirling{n}{k}$ & $\binom{n-1}{k-1}$ & $k!\stirling{n}{k}$ & $\mathrm{p}(n,k)$: \#partitions of $n$ into $k$ positive parts \\
$\mathrm{size}\le 1$ & $[n \le k]$ & $[n \le k]$ & $\binom{k}{n}$ & $n!\binom{k}{n}$ & $[cond]$: $1$ if $cond=true$, else $0$\\
\bottomrule
\end{tabular}
\end{multicols*}
\onecolumn
\begin{multicols*}{3}
\section{Useful Information}
\section{Misc}
\subsection{Debugging Tips}
\begin{myitemize}
\item Stack overflow? Recursive DFS on tree that is actually a long path?
\item Floating-point numbers
\begin{itemize}
\item Getting \texttt{NaN}? Make sure \texttt{acos} etc.\ are
not getting values out of their range (perhaps
\texttt{1+eps}).
\item Rounding negative numbers?
\item Outputting in scientific notation?
\end{itemize}
\item Wrong Answer?
\begin{itemize}
\item Read the problem statement again!
\item Are multiple test cases being handled correctly?
Try repeating the same test case many times.
\item Integer overflow?
\item Think very carefully about boundaries of all input parameters
\item Try out possible edge cases:
\begin{itemize}
\item $n=0, n=-1, n=1, n=2^{31}-1$ or $n=-2^{31}$
\item List is empty, or contains a single element
\item $n$ is even, $n$ is odd
\item Graph is empty, or contains a single vertex
\item Graph is a multigraph (loops or multiple edges)
\item Polygon is concave or non-simple
\end{itemize}
\item Is initial condition wrong for small cases?
\item Are you sure the algorithm is correct?
\item Explain your solution to someone.
\item Are you using any functions that you don't completely understand? Maybe STL functions?
\item Maybe you (or someone else) should rewrite the solution?
\item Can the input line be empty?
\end{itemize}
\item Run-Time Error?
\begin{itemize}
\item Is it actually Memory Limit Exceeded?
\end{itemize}
\end{myitemize}
\subsection{Solution Ideas}
\begin{myitemize}
\item Dynamic Programming
\begin{itemize}
\item Parsing CFGs: CYK Algorithm
\item Drop a parameter, recover from others
\item Swap answer and a parameter
\item When grouping: try splitting in two
\item $2^k$ trick
\item When optimizing
\begin{itemize}
\item Convex hull optimization
\begin{itemize}
\item $\mathrm{dp}[i] = \min_{j<i}\{\mathrm{dp}[j] + b[j] \times a[i]\}$
\item $b[j] \geq b[j+1]$
\item optionally $a[i] \leq a[i+1]$
\item $O(n^2)$ to $O(n)$
\end{itemize}
\item Divide and conquer optimization
\begin{itemize}
\item $\mathrm{dp}[i][j] = \min_{k<j}\{\mathrm{dp}[i-1][k] + C[k][j]\}$
\item $A[i][j] \leq A[i][j+1]$
\item $O(kn^2)$ to $O(kn\log{n})$
\item sufficient: $C[a][c] + C[b][d] \leq C[a][d] + C[b][c]$, $a\leq b\leq c\leq d$ (QI)
\end{itemize}
\item Knuth optimization
\begin{itemize}
\item $\mathrm{dp}[i][j] = \min_{i<k<j}\{\mathrm{dp}[i][k] + \mathrm{dp}[k][j] + C[i][j]\}$
\item $A[i][j-1] \leq A[i][j] \leq A[i+1][j]$
\item $O(n^3)$ to $O(n^2)$
\item sufficient: QI and $C[b][c] \leq C[a][d]$, $a\leq b\leq c\leq d$
\end{itemize}
\end{itemize}
\end{itemize}
\item Greedy
\item Randomized
\item Optimizations
\begin{itemize}
\item Use bitset (/64)
\item Switch order of loops (cache locality)
\end{itemize}
\item Process queries offline
\begin{itemize}
\item Mo's algorithm
\end{itemize}
\item Square-root decomposition