diff --git a/generate_pdf/custom/kactl_template.cpp b/generate_pdf/custom/kactl_template.cpp new file mode 100644 index 0000000..ac5ff71 --- /dev/null +++ b/generate_pdf/custom/kactl_template.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +#define rep(i, a, b) for(int i = a; i < (b); ++i) +#define all(x) begin(x), end(x) +#define sz(x) (int)(x).size() +typedef long long ll; +typedef pair pii; +typedef vector vi; + +int main() { + cin.tie(0)->sync_with_stdio(0); + cin.exceptions(cin.failbit); +} diff --git a/generate_pdf/custom/kactl_troubleshoot.md b/generate_pdf/custom/kactl_troubleshoot.md new file mode 100644 index 0000000..de5e1c6 --- /dev/null +++ b/generate_pdf/custom/kactl_troubleshoot.md @@ -0,0 +1,9 @@ +Pre-submit: Write a few simple test cases if sample is not enough. Are time limits close? If so, generate max cases. Is the memory usage fine? Could anything overflow? Make sure to submit the right file. + +Wrong answer: Print your solution! Print debug output, as well. Are you clearing all data structures between test cases? Can your algorithm handle the whole range of input? Read the full problem statement again. Do you handle all corner cases correctly? Have you understood the problem correctly? Any uninitialized variables? Any overflows? Confusing N and M, i and j, etc.? Are you sure your algorithm works? What special cases have you not thought of? Are you sure the STL functions you use work as you think? Add some assertions, maybe resubmit. Create some testcases to run your algorithm on. Go through the algorithm for a simple case. Go through this list again. Explain your algorithm to a teammate. Ask the teammate to look at your code. Go for a small walk, e.g. to the toilet. Is your output format correct? (including whitespace) Rewrite your solution from the start or let a teammate do it. + +Runtime error: Have you tested all corner cases locally? Any uninitialized variables? Are you reading or writing outside the range of any vector? Any assertions that might fail? Any possible division by 0? (mod 0 for example) Any possible infinite recursion? Invalidated pointers or iterators? Are you using too much memory? Debug with resubmits (e.g. remapped signals, see Various). + +Time limit exceeded: Do you have any possible infinite loops? What is the complexity of your algorithm? Are you copying a lot of unnecessary data? (References) How big is the input and output? (consider scanf) Avoid vector, map. (use arrays/unordered_map) What do your teammates think about your algorithm? + +Memory limit exceeded: What is the max amount of memory your algorithm should need? Are you clearing all data structures between test cases? diff --git a/generate_pdf/custom/python_template.py b/generate_pdf/custom/python_template.py new file mode 100644 index 0000000..afb8054 --- /dev/null +++ b/generate_pdf/custom/python_template.py @@ -0,0 +1,3 @@ +import sys +# Avoid automatic flushes +input = lambda:sys.stdin.readline().rstrip() diff --git a/generate_pdf/custom/useful_stuff_in_python.py b/generate_pdf/custom/useful_stuff_in_python.py new file mode 100644 index 0000000..8fbb75d --- /dev/null +++ b/generate_pdf/custom/useful_stuff_in_python.py @@ -0,0 +1,187 @@ +# --- Fractions --- +from fractions import Fraction + +f = Fraction(6, 8) # Automatically simplifies to 3/4 +print(f) # 3/4 +print(f.numerator, f.denominator) # 3 4 + + +# --- Decimal (High Precision) --- +from decimal import Decimal, getcontext + +getcontext().prec = 30 # Set precision +print(Decimal(1) / Decimal(7)) # 0.142857142857142857142857142857 +print(Decimal('0.1') + Decimal('0.2')) # 0.3 (exact) + + +# --- Output Formatting & Floating Point Decimals --- +val = 5.12399 +print(f'{val:.4f}') # 5.1240 (rounds to 4 decimals) + +# Integer zero-padding example +num = 42 +print(f'{num:05d}') # 00042 (zero-pad with <= 5 zeros) + + +# --- Sys Settings & File / Stream I/O --- +import sys + +# Print with flush +# Note that calling input() also flushes +# Warning: flushing can be slow +print('Hello world', flush=True) + +# Read input without flushing +input = lambda: sys.stdin.readline().rstrip() + +# Reading all tokenized inputs at once. +# Fastest way to read input in Python +# input_data = sys.stdin.read().split() + +# Increase recursion depth for deep DFS +# Warning: May not work as expected! +# Consider making use of bootstrap.py instead +sys.setrecursionlimit(200000) + +# Allow huge int-to-string conversions +# Default limit is 4300 digits +sys.set_int_max_str_digits(100000) + +# Print to stderr +print('Debug message', file=sys.stderr) + +# Reading from / Writing to files: +# with open('input.txt', 'r') as fin, open('output.txt', 'w') as fout: +# content = fin.read() +# fout.write(content) + + +# --- Heapq (Min-Heap) --- +import heapq + +h = [3, 1, 4, 1, 5, 9] +heapq.heapify(h) # Transforms list into a min-heap in-place +heapq.heappush(h, 2) # Push item +print(heapq.heappop(h)) # Pop smallest item +print(h[0]) # Peek at current smallest item + + +# --- Collections (defaultdict, Counter) --- +from collections import Counter, defaultdict + +d = defaultdict(list) +d['key'].append(10) +print(d['key']) # [10] +print(d['missing_key']) # [] (automatically created) + +c = Counter('abracadabra') +print(c) # Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}) +print(c.most_common(2)) # [('a', 5), ('b', 2)] + + +# --- Random --- +import random + +random.seed(42) +print(random.randint(1, 10)) # Random int [a, b] inclusive +print(random.randrange(0, 10, 2)) # Random from range(start, stop, step) +print(random.choice(['apple', 'banana', 'cherry'])) # Random element from sequence +print(random.uniform(1.0, 5.0)) # Random float between a and b inclusive +print(random.getrandbits(8)) # Random integer with k random bits + + +# --- Itertools --- +import itertools + +P = itertools.permutations('ABC', 2) +print(*P) # ('A', 'B') ('A', 'C') ('B', 'A') ('B', 'C') ('C', 'A') ('C', 'B') + +C = itertools.combinations('ABC', 2) +print(*C) # ('A', 'B') ('A', 'C') ('B', 'C') + +CR = itertools.combinations_with_replacement('ABC', 2) +print(*CR) # ('A', 'A') ('A', 'B') ('A', 'C') ('B', 'B') ('B', 'C') ('C', 'C') + + +# --- Memoisation --- +from functools import cache + +@cache # Automatically memoises return values +def fib(n): + return n if n < 2 else fib(n - 1) + fib(n - 2) + +print(fib(50)) + + +# --- Bit Operations (bit_length, bit_count) --- +x = 5 # in binary 0b101 +print(x.bit_length()) # 3 (number of bits needed to represent x) +print(x.bit_count()) # 2 (number of set bits / popcount, Python 3.10+) + + +# --- Alphabet & Character Checks --- +import string + +print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz +print(string.ascii_uppercase) # ABCDEFGHIJKLMNOPQRSTUVWXYZ + +vowels = set('aeiouAEIOU') +print('a' in vowels) # True +print('b'.isalpha()) # True + +s = ' hello world ' +print(s.strip()) # 'hello world' (strips both ends) +print(s.lstrip()) # 'hello world ' (strips left) +print(s.rstrip()) # ' hello world' (strips right) + +words = ['apple', 'banana', 'cherry'] +print('-'.join(words)) # 'apple-banana-cherry' + +print('42'.ljust(5, '0')) # '42000' (left-justified, padded) +print('42'.rjust(5, '0')) # '00042' (right-justified, padded) + +text = 'apple,banana,cherry' +print(text.split(',')) # ['apple', 'banana', 'cherry'] (with argument) +print(' a b c '.split()) # ['a', 'b', 'c'] (without argument, splits by whitespace) + +# Substring check & counting +print('ana' in 'banana') # True (checks if substring exists) +print('bananana'.count('ana')) # 2 (counts non-overlapping occurrences: b[ana]n[ana]) + + +# --- Bisect (Binary Search) --- +import bisect + +arr = [1, 3, 3, 3, 5, 7] +print(bisect.bisect_left(arr, 3)) # 1 (first insertion index) +print(bisect.bisect_right(arr, 3)) # 4 (last insertion index) + + +# --- Math Module & Division Tricks --- +import math + +print(math.pi) # 3.141592653589793 +print(math.gcd(24, 36)) # 12 +print(math.lcm(4, 6)) # 12 +print(math.comb(5, 2)) # 10 (nCr / binomial coefficient) +print(math.perm(5, 2)) # 20 (nPr) +print(math.isqrt(10)) # 3 (integer square root) +print(math.inf) # Infinity representation + +# Division types: +a, b = 7, 3 +print(a // b) # 2 (floored division) +print(0 -- a // b)) # 3 (ceiled division) + + +# --- Prime Sieve (Sieve of Eratosthenes) --- +limit = 30 +is_prime = [1] * limit +is_prime[0] = is_prime[1] = 0 +for i in range(2, int(limit**0.5) + 1): + if is_prime[i]: + for j in range(i * i, limit, i): + is_prime[j] = 0 + +primes = [i for i, p in enumerate(is_prime) if p] +print(primes) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] diff --git a/generate_pdf/main.pdf b/generate_pdf/main.pdf new file mode 100644 index 0000000..2f94da4 Binary files /dev/null and b/generate_pdf/main.pdf differ diff --git a/generate_pdf/main.tex b/generate_pdf/main.tex new file mode 100644 index 0000000..e64891e --- /dev/null +++ b/generate_pdf/main.tex @@ -0,0 +1,468 @@ +\documentclass[a4paper,10pt]{article} + +% ============================================================ +% Packages +% ============================================================ + +\usepackage[ + a4paper, + landscape, + margin=10mm +]{geometry} + +\usepackage{lmodern} +\usepackage{multicol} +\usepackage{xcolor} +\usepackage{listings} +\usepackage{fancyhdr} +\usepackage{graphicx} + +% ============================================================ +% Layout +% ============================================================ + +\setlength{\columnsep}{3mm} +\setlength{\parindent}{0pt} +\setlength{\parskip}{0pt} + +% ============================================================ +% Pyrival root directory +% ============================================================ + +\newcommand{\pyrivalroot}{../pyrival/} +\newcommand{\customroot}{./custom/} + +% ============================================================ +% Colors +% ============================================================ + +\definecolor{codewhite}{RGB}{255,255,255} +\definecolor{codegray}{RGB}{245,245,245} +\definecolor{commentgray}{RGB}{100,100,100} +\definecolor{keywordblue}{RGB}{0,60,140} +\definecolor{stringred}{RGB}{140,30,30} + +% ============================================================ +% Code font +% ============================================================ + +\newcommand{\codefont}{% + \ttfamily + \fontsize{8pt}{9pt}\selectfont +} + +% ============================================================ +% Code listings +% ============================================================ + +\lstdefinestyle{python}{ + language=Python, + basicstyle=\codefont, + keywordstyle=\color{keywordblue}\bfseries, + commentstyle=\color{commentgray}, + stringstyle=\color{stringred}, + backgroundcolor=\color{codewhite}, + numbers=none, + frame=single, + framerule=0.2pt, + rulecolor=\color{black!25}, + breaklines=true, + breakatwhitespace=true, + columns=fullflexible, + breakindent=0pt, + keepspaces=true, + showstringspaces=false, + % Tabs are displayed as 2 spaces. + tabsize=2, + % Four spaces in the source are displayed as two spaces. + %literate={\ \ }{{\ }}2, + literate={\ \ }{{\ }}2 {~}{{\textasciitilde}}1, + % Mark lines that have been wrapped around + prebreak=\mbox{\textcolor{red}\textbackslash}, + aboveskip=2pt, + belowskip=4pt +} + +\lstdefinestyle{cpp}{ + language=C++, + basicstyle=\codefont, + keywordstyle=\color{keywordblue}\bfseries, + commentstyle=\color{commentgray}, + stringstyle=\color{stringred}, + backgroundcolor=\color{codewhite}, + numbers=none, + frame=single, + framerule=0.2pt, + rulecolor=\color{black!25}, + breaklines=true, + breakatwhitespace=true, + columns=fullflexible, + breakindent=0pt, + keepspaces=true, + showstringspaces=false, + % Tabs are displayed as 2 spaces. + tabsize=2, + % Four spaces in the source are displayed as two spaces. + %literate={\ \ }{{\ }}2, + literate={\ \ }{{\ }}2 {~}{{\textasciitilde}}1, + % Mark lines that have been wrapped around + prebreak=\mbox{\textcolor{red}\textbackslash}, + aboveskip=2pt, + belowskip=4pt +} + +\lstdefinelanguage{vim}{ + % list of keywords + morekeywords={ + set, let + map, nmap, + filetype, + on, off, + autocmd, + Plugin, + call, + }, + morecomment=[l]{"}, % l is for line comment + morestring=[b]' % defines that strings are enclosed in double quotes +} + +\lstdefinestyle{vimrc}{ + language=vim, + basicstyle=\codefont, + keywordstyle=\color{keywordblue}\bfseries, + commentstyle=\color{commentgray}, + stringstyle=\color{stringred}, + backgroundcolor=\color{codewhite}, + numbers=none, + frame=single, + framerule=0.2pt, + rulecolor=\color{black!25}, + breaklines=true, + breakatwhitespace=true, + columns=fullflexible, + breakindent=0pt, + keepspaces=true, + showstringspaces=false, + % Tabs are displayed as 2 spaces. + tabsize=2, + % Four spaces in the source are displayed as two spaces. + %literate={\ \ }{{\ }}2, + literate={\ \ }{{\ }}2 {~}{{\textasciitilde}}1, + % Mark lines that have been wrapped around + prebreak=\mbox{\textcolor{red}\textbackslash}, + aboveskip=2pt, + belowskip=4pt +} + +\lstdefinestyle{markdown}{ + language=html, + basicstyle=\codefont, + keywordstyle=\color{keywordblue}\bfseries, + commentstyle=\color{commentgray}, + stringstyle=\color{stringred}, + backgroundcolor=\color{codewhite}, + numbers=none, + frame=single, + framerule=0.2pt, + rulecolor=\color{black!25}, + breaklines=true, + breakatwhitespace=true, + columns=fullflexible, + breakindent=0pt, + keepspaces=true, + showstringspaces=false, + % Tabs are displayed as 2 spaces. + tabsize=2, + % Four spaces in the source are displayed as two spaces. + %literate={\ \ }{{\ }}2, + literate={\ \ }{{\ }}2 {~}{{\textasciitilde}}1, + % Mark lines that have been wrapped around + prebreak=\mbox{\textcolor{red}\textbackslash}, + aboveskip=2pt, + belowskip=4pt +} + +% ============================================================ +% File heading +% ============================================================ + +\newcommand{\codefile}[1]{% + \vspace{3pt}% + \parbox{\linewidth}{% + \raggedright + {\small\bfseries\ttfamily\detokenize{#1}}% + }% + \par\vspace{1pt}% +} + +% ============================================================ +% Compact Table of Contents +% ============================================================ + +% We use LaTeX's normal .toc mechanism, but make the entries +% extremely compact. + +\makeatletter + +% Remove the normal "Contents" heading formatting. +% We don't want a "Contents" heading at all. +\renewcommand{\tableofcontents}{% + \@starttoc{toc}% +} + +% Make each TOC entry tiny and compact. +%\renewcommand{\l@section}[2]{% +% \noindent +% \ttfamily\scriptsize +% #1% +% \leaders\hbox to 1em{\hss.\hss}\hfill +% #2% +% \par +%} +\renewcommand{\l@section}[2]{% + \noindent + \makebox[0.70\columnwidth][l]{% + \ttfamily\scriptsize + #1% + \leaders\hbox to 1.0em{\hss.\hss}\hfill + }% + \ttfamily\scriptsize #2% + \par +} + + +%% No indentation or extra vertical space. +%\setlength{\tocloftparskip}{0pt} + +\makeatother + +% ============================================================ +% Include code file +% +% Every file automatically adds itself to the TOC. +% ============================================================ + +\usepackage{xstring} + +\newcommand{\mylisting}[2]{% + \IfEndWith{#1}{.py}{% + \lstinputlisting[style=python,inputpath=#2]{#1}% + }{% + \IfEndWith{#1}{.cpp}{% + \lstinputlisting[style=cpp,inputpath=#2]{#1}% + }{% + \IfEndWith{#1}{.h}{% + \lstinputlisting[style=cpp,inputpath=#2]{#1}% + }{% + \IfEndWith{#1}{.vimrc}{% + \lstinputlisting[style=vimrc,inputpath=#2]{#1}% + }{% + \IfEndWith{#1}{.md}{% + \lstinputlisting[style=markdown,inputpath=#2]{#1}% + }{% + \PackageError{myfile}{Unknown file extension}{% + No listings style is defined for `#1'.% + }% + }% + }% + }% + }% + }% +} + + +\newcommand{\pyrivalfile}[1]{% + % + % Add this file to the table of contents. + % + \addcontentsline{toc}{section}{\protect\detokenize{#1}}% + % + % Print the file itself. + % + \codefile{#1}% + \mylisting{#1}{\pyrivalroot}% +} + +\newcommand{\customfile}[1]{% + % + % Add this file to the table of contents. + % + \addcontentsline{toc}{section}{\protect\detokenize{#1}}% + % + % Print the file itself. + % + \codefile{#1}% + \mylisting{#1}{\customroot}% +} + +% ============================================================ +% Page style +% ============================================================ + +\pagestyle{fancy} +\fancyhf{} + +\renewcommand{\headrulewidth}{0pt} + +% Page number in the top-right corner. +% Lowered slightly so it is not cut off. +\fancyhead[R]{\raisebox{-10mm}{\scriptsize\thepage}} + +% ============================================================ +% Document +% ============================================================ + +\begin{document} + +% ============================================================ +% TITLE PAGE +% ============================================================ + +\thispagestyle{empty} + +%\vspace*{\fill} + +\begin{center} + \includegraphics[scale=0.2]{pythonk.png} + \vspace{2em} + + {\Huge\bfseries Python TRD for ICPC Competitions based on Pyrival}\\[5mm] + %{\huge Made by Pajenegod} +\end{center} + +\vspace*{\fill} + +\begin{center} + \today +\end{center} + +\newpage + +% ============================================================ +% CODE +% +% The title page is excluded from the numbered reference. +% The first page after the title page is page 1. +% ============================================================ + +\setcounter{page}{1} + +% ============================================================ +% TABLE OF CONTENTS +% ============================================================ + +\begin{multicols*}{3} + +\tableofcontents +%\vspace{4pt} + +% ============================================================ +% Contest +% ============================================================ + +\customfile{python_template.py} +\customfile{.vimrc} +\customfile{kactl_template.cpp} +\customfile{kactl_troubleshoot.md} +\customfile{useful_stuff_in_python.py} + +% ============================================================ +% Algebra +% ============================================================ + +\pyrivalfile{algebra/discrete_log.py} +\pyrivalfile{algebra/factors.py} +\pyrivalfile{algebra/fft.py} +\pyrivalfile{algebra/mod_sqrt.py} +%\pyrivalfile{algebra/mod_template.py} +\pyrivalfile{algebra/modinv.py} +\pyrivalfile{algebra/ntt.py} +\pyrivalfile{algebra/phi.py} + +% ============================================================ +% Combinatorics +% ============================================================ + +\pyrivalfile{combinatorics/nCr_mod.py} + +% ============================================================ +% Data Structures +% ============================================================ + +\pyrivalfile{data_structures/DisjointSetUnion.py} +\pyrivalfile{data_structures/FenwickTree.py} +\pyrivalfile{data_structures/LazySegmentTree.py} +\pyrivalfile{data_structures/RangeMinimumQuery.py} +\pyrivalfile{data_structures/SortedList.py} +\pyrivalfile{data_structures/TwoSat.py} + +% ============================================================ +% Geometry +% ============================================================ + +\pyrivalfile{geometry/convex_hull.py} + +% ============================================================ +% Graphs +% ============================================================ + +\pyrivalfile{graphs/bellman_ford.py} +\pyrivalfile{graphs/bfs.py} +\pyrivalfile{graphs/dfs.py} +\pyrivalfile{graphs/binary_lifting_on_tree.py} +\pyrivalfile{graphs/bridges_and_articulation_points.md} +\pyrivalfile{graphs/centroid_decomposition.py} +%\pyrivalfile{graphs/centroid_shallowest_decomposition.py} +\pyrivalfile{graphs/dijkstra.py} +\pyrivalfile{graphs/dinic.py} +\pyrivalfile{graphs/edge_bcc.py} +\pyrivalfile{graphs/floyd_warshall.py} +\pyrivalfile{graphs/heavy_light.py} +\pyrivalfile{graphs/hopcroft_karp.py} +\pyrivalfile{graphs/kruskal.py} +\pyrivalfile{graphs/lca.py} +\pyrivalfile{graphs/maximum_matching.py} +\pyrivalfile{graphs/node_bcc.py} +\pyrivalfile{graphs/prim.py} +\pyrivalfile{graphs/rerooter.py} +\pyrivalfile{graphs/scc.py} +\pyrivalfile{graphs/toposort.py} + +% ============================================================ +% Linear algebra +% ============================================================ + +\pyrivalfile{linear_algebra/matrix.py} +\pyrivalfile{linear_algebra/max_xor.py} + +% ============================================================ +% Misc +% ============================================================ + +\pyrivalfile{misc/bit_hacks.py} +\pyrivalfile{misc/bootstrap.py} +\pyrivalfile{misc/mod.py} + +% ============================================================ +% Numerical +% ============================================================ + +\pyrivalfile{numerical/berlekamp_massey.py} +\pyrivalfile{numerical/integrate.py} +\pyrivalfile{numerical/search.py} + +% ============================================================ +% Strings +% ============================================================ + +\pyrivalfile{strings/LPSubstr.py} +\pyrivalfile{strings/hashing.py} +\pyrivalfile{strings/kmp.py} +\pyrivalfile{strings/lcs.py} +\pyrivalfile{strings/min_rotation.py} +\pyrivalfile{strings/suffix_array.py} +\pyrivalfile{strings/z_algorithm.py} + +\end{multicols*} + +\end{document} diff --git a/generate_pdf/pythonk.png b/generate_pdf/pythonk.png new file mode 100644 index 0000000..6b39672 Binary files /dev/null and b/generate_pdf/pythonk.png differ