Skip to content

Commit 3a94b90

Browse files
committed
merged
2 parents 5f4ff5a + 9245396 commit 3a94b90

File tree

228 files changed

+3381
-1591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+3381
-1591
lines changed

.github/workflows/ccpp.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: C/C++ CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 20
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Install pdflatex
18+
run: sudo apt-get install texlive-latex-base texlive-latex-recommended texlive-latex-extra
19+
- name: Build pdf
20+
run: make kactl
21+
- name: Run compiles
22+
run: make test-compiles
23+
- name: Run stress tests
24+
run: make test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
*
2+
!*/
3+
!*.*
14
/build/
25
a.out
36
header.tmp

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ help:
66
@echo "This makefile builds KACTL (KTH Algorithm Competition Template Library)"
77
@echo ""
88
@echo "Available commands are:"
9-
@echo " make fast - to build KACTL, quickly (only runs LaTeX once)"
10-
@echo " make kactl - to build KACTL"
11-
@echo " make clean - to clean up the build process"
12-
@echo " make veryclean - to clean up and remove kactl.pdf"
13-
@echo " make help - to show this information"
9+
@echo " make fast - to build KACTL, quickly (only runs LaTeX once)"
10+
@echo " make kactl - to build KACTL"
11+
@echo " make clean - to clean up the build process"
12+
@echo " make veryclean - to clean up and remove kactl.pdf"
13+
@echo " make test - to run all the stress tests in stress-tests/"
14+
@echo " make test-compiles - to test compiling all headers"
15+
@echo " make help - to show this information"
16+
@echo " make showexcluded - to show files that are not included in the doc"
1417
@echo ""
1518
@echo "For more information see the file 'doc/README'"
1619

@@ -22,7 +25,7 @@ kactl: test-session.pdf | build
2225
$(LATEXCMD) content/kactl.tex && $(LATEXCMD) content/kactl.tex
2326
cp build/kactl.pdf kactl.pdf
2427

25-
clean:
28+
clean:
2629
cd build && rm -f kactl.aux kactl.log kactl.tmp kactl.toc kactl.pdf kactl.ptc
2730

2831
veryclean: clean
@@ -33,6 +36,16 @@ veryclean: clean
3336
build:
3437
mkdir -p build/
3538

39+
test:
40+
./doc/scripts/run-all.sh .
41+
42+
test-compiles:
43+
./doc/scripts/compile-all.sh .
44+
3645
test-session.pdf: content/test-session/test-session.tex content/test-session/chapter.tex | build
3746
$(LATEXCMD) content/test-session/test-session.tex
3847
cp build/test-session.pdf test-session.pdf
48+
49+
showexcluded: build
50+
grep -RoPh '^\s*\\kactlimport{\K.*' content/ | sed 's/.$$//' > build/headers_included
51+
find ./content -name "*.h" -o -name "*.py" -o -name "*.java" | grep -vFf build/headers_included

README.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,65 @@
11
# KACTL
22

3-
This repo hosts KACTL, KTH's ICPC team reference document.
4-
It consists of 25 pages of copy-paste:able code, for use in ICPC-style programming competitions.
3+
This repo hosts KACTL, [KTH](https://en.wikipedia.org/wiki/KTH_Royal_Institute_of_Technology)'s ICPC team reference document.
4+
It consists of 25 pages of copy-pasteable C++ code, for use in ICPC-style programming competitions.
55

66
See [kactl.pdf](https://github.com/kth-competitive-programming/kactl/blob/master/kactl.pdf) for the final, browsable version,
77
and [content/](https://github.com/kth-competitive-programming/kactl/tree/master/content) for raw source code.
88

99
## Aspirations
1010

11-
KACTL algorithms should be: useful, short, fast enough, readable, and if relevant, easy to modify.
12-
Short and readable sometimes conflict -- usually then short takes precedence, although
13-
the algorithms should still be made easy to type in and hard to make typos in (since ICPC-style contests
14-
require you to copy them from paper).
15-
11+
KACTL algorithms should be: useful, short, fast enough, well tested, and if relevant, readable and easy to modify.
1612
They should *not* be overly generic, since code is manually typed and that just adds overhead.
1713
Due to space issues, we also exclude algorithms that are very common/simple (e.g., Dijkstra), or very uncommon (general weighted matching).
1814

19-
If you feel that something is missing, could be cleaned up, or notice a bug, please file an issue or send us a pull request!
15+
If you feel that something is missing, could be cleaned up, or notice a bug, please file an issue or [send a pull request](https://help.github.com/articles/fork-a-repo/)!
2016

21-
## Hacking on KACTL
17+
## Customizing KACTL
2218

23-
For coding style, try to copy existing code.
24-
Each algorithm should contain a header with the author of the code, the date it
25-
was added, a description of the algorithm, its testing status, and preferably also
26-
source, license and time complexity.
27-
Line width is 63 chars, with tabs for indentation (tab = 2 spaces in the pdf).
19+
While KACTL is usable as is, it's also easy to modify if you want to create a personalized copy.
20+
In particular, you may want to change the cover page, or make your own choice of algorithms to include --
21+
due to space concerns, not all algorithms in the repo are included in the pdf.
22+
You may also want to enable colored syntax highlighting.
2823

29-
When adding/removing files, edit the corresponding `chapter.tex` file as well.
30-
`chapter.tex` also contains all non-source code, e.g. math and textual descriptions.
31-
For nicer alignment you might want to use `\hardcolumnbreak`, `\columnbreak` or `\newpage` commands,
24+
`content/kactl.tex` is the main file of KACTL, and can be edited to change team name, logo, syntax highlighting, etc.
25+
It imports `chapter.tex` files from each of the `content/` subdirectories, which define the contents of each chapter.
26+
These include source code, text and math in the form of LaTeX.
27+
To add/remove code from a chapter, add/remove a corresponding `\kactlimport` line from the `chapter.tex` file.
28+
For nicer alignment you might want to insert `\hardcolumnbreak`, `\columnbreak` or `\newpage` commands,
3229
though this is usually only done before important contests, and not on master.
30+
The algorithms that are not included in the pdf are left commented out in `chapter.tex`.
3331

34-
To build KACTL, type `make kactl` (or `make fast`) on a \*NIX machine -- this will update `kactl.pdf`.
32+
To build KACTL, type `make kactl` (or `make fast`) on a \*nix machine -- this will update `kactl.pdf`.
3533
(Windows might work as well, but is not tested.) `doc/README` has a few more notes about this.
3634

37-
kactl.pdf is to be kept to 25 pages + cover page.
38-
Occasionally the generated kactl.pdf is committed to the repo for convenience, but not too often because it makes git operations slower.
35+
Tips:
36+
1. Check out what's excluded by default by running `make showexcluded`.
37+
The default configuration is chosen to be a reasonable balance for beginners
38+
and advanced teams.
39+
2. Take advantage of the hashing when typing in these algorithms. Each
40+
algorithm has a 6 character MD5 hash in the upper right. This hash can be
41+
generated by using `hash.sh` or the `:Hash` command from the `.vimrc`. The
42+
hashing ignores whitespace and comments.
3943

40-
Before printing KACTL for an official contest, you may want to locally change the arguments to `\team`, `\contest`, etc. in build/kactl.tex to something more fitting,
41-
You may also enable colored syntax highlighting in the same file.
44+
## Coding style
4245

43-
To upstream your changes, [send a pull request](https://help.github.com/articles/fork-a-repo/).
46+
KACTL uses a relatively terse coding style, with a handful of macros/typedefs defined in the
47+
[template](https://github.com/kth-competitive-programming/kactl/blob/master/content/contest/template.cpp) that help shorten the code.
48+
Line width is 63 chars, with tabs for indentation (tab = 2 spaces in the pdf).
49+
50+
Each algorithm contains a header with the author of the code, the date it
51+
was added, a description of the algorithm, its testing status, and preferably also
52+
source, license and time complexity.
53+
54+
kactl.pdf is to be kept to 25 pages + cover page.
55+
Occasionally the generated kactl.pdf is committed to the repo for convenience, but not too often because it makes git operations slower.
4456

4557
## Testing
4658

47-
KACTL algorithms are tested manually, on online judges and/or using fuzz-tests (see the `fuzz-tests` directory).
48-
Simple variable renamings etc. don't need testing, but it's prudent to at least run `./test-compiles.sh file.h` to make sure the file still compiles.
59+
KACTL aims for a high level of confidence in algorithm correctness.
60+
Testing is done both on online judges and (for newer algorithms) with stress tests
61+
that compare output to a more naive algorithm for a large amount of randomly generated cases.
62+
These tests live in the `stress-tests` directory, and are run with CI on every commit. The CI also verifies that all headers compile (except for a whitelist in `docs/scripts/skip_headers`) and that the latex compiles.
4963

5064
`old-unit-tests` contains a couple of broken unit tests, last touched about ten years ago.
5165

@@ -57,4 +71,4 @@ Many source files are marked with license (we try to go with
5771
Presumably good will is to be assumed from other authors, though, and in many cases permission should not be needed since the code is not distributed.
5872
To help trace things back, sources and authors are noted in source files.
5973

60-
Everything in `fuzz-tests` and `build` is implicitly CC0.
74+
Everything in `stress-tests` is implicitly CC0, except reference implementations taken from around the Internet.

content/combinatorial/IntPerm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* Date: 2018-07-06
44
* License: CC0
55
* Description: Permutation -> integer conversion. (Not order preserving.)
6+
* Integer -> permutation can use a lookup table.
67
* Time: O(n)
78
*/
89
#pragma once
910

1011
int permToInt(vi& v) {
1112
int use = 0, i = 0, r = 0;
12-
trav(x, v) r = r * ++i + __builtin_popcount(use & -(1 << x)),
13+
for(int x:v) r = r * ++i + __builtin_popcount(use & -(1<<x)),
1314
use |= 1 << x; // (note: minus, not ~!)
1415
return r;
1516
}

content/combinatorial/binomialModPrime.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

content/combinatorial/chapter.tex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,28 @@ \section{Partitions and subsets}
3434
\end{tabular}
3535
\end{center}
3636

37+
\subsection{Lucas' Theorem}
38+
Let $n,m$ be non-negative integers and $p$ a prime. Write $n=n_kp^k+...+n_1p+n_0$ and $m=m_kp^k+...+m_1p+m_0$. Then $\binom{n}{m} \equiv \prod_{i=0}^k\binom{n_i}{m_i} \pmod{p}$.
39+
3740
\subsection{Binomials}
38-
\kactlimport{binomialModPrime.h}
3941
\kactlimport{multinomial.h}
4042

4143
\section{General purpose numbers}
44+
\subsection{Bernoulli numbers}
45+
EGF of Bernoulli numbers is $B(t)=\frac{t}{e^t-1}$ (FFT-able).
46+
$B[0,\ldots] = [1, -\frac{1}{2}, \frac{1}{6}, 0, -\frac{1}{30}, 0, \frac{1}{42}, \ldots]$
47+
48+
Sums of powers:
49+
\small
50+
\[ \sum_{i=1}^n n^m = \frac{1}{m+1} \sum_{k=0}^m \binom{m+1}{k} B_k \cdot (n+1)^{m+1-k} \]
51+
\normalsize
52+
53+
Euler-Maclaurin formula for infinite sums:
54+
\small
55+
\[ \sum_{i=m}^{\infty} f(i) = \int_m^\infty f(x) dx - \sum_{k=1}^\infty \frac{B_k}{k!}f^{(k-1)}(m) \]
56+
\[ \approx \int_{m}^\infty f(x)dx + \frac{f(m)}{2} - \frac{f'(m)}{12} + \frac{f'''(m)}{720} + O(f^{(5)}(m)) \]
57+
\normalsize
58+
4259
\subsection{Stirling numbers of the first kind}
4360
Number of permutations on $n$ items with $k$ cycles.
4461
\begin{align*}
@@ -65,6 +82,11 @@ \section{General purpose numbers}
6582
$1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, \dots$. For $p$ prime,
6683
\[ B(p^m+n)\equiv mB(n)+B(n+1) \pmod{p} \]
6784

85+
\subsection{Labeled unrooted trees}
86+
\# on $n$ vertices: $n^{n-2}$ \\
87+
\# on $k$ existing trees of size $n_i$: $n_1n_2\cdots n_k n^{k-2}$ \\
88+
\# with degrees $d_i$: $(n-2)! / ((d_1-1)! \cdots (d_n-1)!)$
89+
6890
\subsection{Catalan numbers}
6991
\[ C_n=\frac{1}{n+1}\binom{2n}{n}= \binom{2n}{n}-\binom{2n}{n+1} = \frac{(2n)!}{(n+1)!n!} \]
7092
\[ C_0=1,\ C_{n+1} = \frac{2(2n+1)}{n+2}C_n,\ C_{n+1}=\sum C_iC_{n-i} \]

content/contest/template.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
using namespace std;
33

44
#define rep(i, a, b) for(int i = a; i < (b); ++i)
5-
#define trav(a, x) for(auto& a : x)
65
#define all(x) begin(x), end(x)
76
#define sz(x) (int)(x).size()
87
typedef long long ll;
98
typedef pair<int, int> pii;
109
typedef vector<int> vi;
1110

1211
int main() {
13-
cin.sync_with_stdio(0); cin.tie(0);
12+
cin.tie(0)->sync_with_stdio(0);
1413
cin.exceptions(cin.failbit);
1514
}

content/contest/troubleshoot.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Pre-submit:
2-
Write a few simple test cases, if sample is not enough.
2+
Write a few simple test cases if sample is not enough.
33
Are time limits close? If so, generate max cases.
44
Is the memory usage fine?
55
Could anything overflow?
66
Make sure to submit the right file.
77

88
Wrong answer:
99
Print your solution! Print debug output, as well.
10-
Are you clearing all datastructures between test cases?
10+
Are you clearing all data structures between test cases?
1111
Can your algorithm handle the whole range of input?
1212
Read the full problem statement again.
1313
Do you handle all corner cases correctly?
@@ -22,11 +22,11 @@ Add some assertions, maybe resubmit.
2222
Create some testcases to run your algorithm on.
2323
Go through the algorithm for a simple case.
2424
Go through this list again.
25-
Explain your algorithm to a team mate.
26-
Ask the team mate to look at your code.
25+
Explain your algorithm to a teammate.
26+
Ask the teammate to look at your code.
2727
Go for a small walk, e.g. to the toilet.
2828
Is your output format correct? (including whitespace)
29-
Rewrite your solution from the start or let a team mate do it.
29+
Rewrite your solution from the start or let a teammate do it.
3030

3131
Runtime error:
3232
Have you tested all corner cases locally?
@@ -45,8 +45,8 @@ What is the complexity of your algorithm?
4545
Are you copying a lot of unnecessary data? (References)
4646
How big is the input and output? (consider scanf)
4747
Avoid vector, map. (use arrays/unordered_map)
48-
What do your team mates think about your algorithm?
48+
What do your teammates think about your algorithm?
4949

5050
Memory limit exceeded:
5151
What is the max amount of memory your algorithm should need?
52-
Are you clearing all datastructures between test cases?
52+
Are you clearing all data structures between test cases?

content/data-structures/FenwickTree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Description: Computes partial sums a[0] + a[1] + ... + a[pos - 1], and updates single elements a[i],
77
* taking the difference between the old and new value.
88
* Time: Both operations are $O(\log N)$.
9+
* Status: Stress-tested
910
*/
1011
#pragma once
1112

content/data-structures/FenwickTree2d.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Description: Computes sums a[i,j] for all i<I, j<J, and increases single elements a[i,j].
77
* Requires that the elements to be updated are known in advance (call fakeUpdate() before init()).
88
* Time: $O(\log^2 N)$. (Use persistent segment trees for $O(\log N)$.)
9+
* Status: stress-tested
910
*/
1011
#pragma once
1112

@@ -18,7 +19,7 @@ struct FT2 {
1819
for (; x < sz(ys); x |= x + 1) ys[x].push_back(y);
1920
}
2021
void init() {
21-
trav(v, ys) sort(all(v)), ft.emplace_back(sz(v));
22+
for (vi& v : ys) sort(all(v)), ft.emplace_back(sz(v));
2223
}
2324
int ind(int x, int y) {
2425
return (int)(lower_bound(all(ys[x]), y) - ys[x].begin()); }

content/data-structures/HashMap.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* Date: 2018-07-23
44
* License: CC0
55
* Source: http://codeforces.com/blog/entry/60737
6-
* Description: Hash map with the same API as unordered\_map, but \tilde 3x faster.
6+
* Description: Hash map with mostly the same API as unordered\_map, but \tilde
7+
* 3x faster. Uses 1.5x memory.
78
* Initial capacity must be a power of 2 (if provided).
89
*/
910
#pragma once
1011

1112
#include <bits/extc++.h> /** keep-include */
1213
// To use most bits rather than just the lowest ones:
13-
struct chash {
14-
const uint64_t C = ll(2e18 * M_PI) + 71; // large odd number
14+
struct chash { // large odd number for C
15+
const uint64_t C = ll(4e18 * acos(0)) | 71;
1516
ll operator()(ll x) const { return __builtin_bswap64(x*C); }
1617
};
1718
__gnu_pbds::gp_hash_table<ll,int,chash> h({},{},{},{},{1<<16});
@@ -20,7 +21,7 @@ __gnu_pbds::gp_hash_table<ll,int,chash> h({},{},{},{},{1<<16});
2021
2122
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
2223
struct chash { // To use most bits rather than just the lowest ones:
23-
const uint64_t C = ll(2e18 * M_PI) + 71; // large odd number
24+
const uint64_t C = ll(4e18 * acos(0)) | 71; // large odd number
2425
ll operator()(ll x) const { return __builtin_bswap64((x^RANDOM)*C); }
2526
};
2627
__gnu_pbds::gp_hash_table<ll, int, chash> h({},{},{},{}, {1 << 16});

content/data-structures/LazySegmentTree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Use with a bump allocator for better performance, and SmallPtr or implicit indices to save memory.
99
* Time: O(\log N).
1010
* Usage: Node* tr = new Node(v, 0, sz(v));
11-
* Status: fuzz-tested a bit
11+
* Status: stress-tested a bit
1212
*/
1313
#pragma once
1414

content/data-structures/LineContainer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* License: CC0
55
* Source: own work
66
* Description: Container where you can add lines of the form kx+m, and query maximum values at points x.
7-
* Useful for dynamic programming.
7+
* Useful for dynamic programming (``convex hull trick'').
88
* Time: O(\log N)
9-
* Status: tested
9+
* Status: stress-tested
1010
*/
1111
#pragma once
1212

0 commit comments

Comments
 (0)