cphb/chapter01.tex

991 lines
29 KiB
TeX
Raw Normal View History

2016-12-28 23:54:51 +01:00
\chapter{Introduction}
Competitive programming combines two topics:
2017-01-30 19:39:11 +01:00
(1) the design of algorithms and (2) the implementation of algorithms.
2016-12-28 23:54:51 +01:00
The \key{design of algorithms} consists of problem solving
and mathematical thinking.
Skills for analyzing problems and solving them
2017-02-13 20:42:16 +01:00
creatively are needed.
2016-12-28 23:54:51 +01:00
An algorithm for solving a problem
has to be both correct and efficient,
and the core of the problem is often
about inventing an efficient algorithm.
2016-12-28 23:54:51 +01:00
Theoretical knowledge of algorithms
2017-05-17 20:23:33 +02:00
is important to competitive programmers.
2017-01-30 20:35:46 +01:00
Typically, a solution to a problem is
2016-12-28 23:54:51 +01:00
a combination of well-known techniques and
new insights.
The techniques that appear in competitive programming
also form the basis for the scientific research
of algorithms.
The \key{implementation of algorithms} requires good
programming skills.
In competitive programming, the solutions
are graded by testing an implemented algorithm
using a set of test cases.
Thus, it is not enough that the idea of the
algorithm is correct, but the implementation also
has to be correct.
2016-12-28 23:54:51 +01:00
A good coding style in contests is
2016-12-28 23:54:51 +01:00
straightforward and concise.
Programs should be written quickly,
2016-12-28 23:54:51 +01:00
because there is not much time available.
Unlike in traditional software engineering,
2017-12-10 11:06:32 +01:00
the programs are short (usually at most a few
hundred lines of code), and they do not need to
be maintained after the contest.
2016-12-28 23:54:51 +01:00
\section{Programming languages}
\index{programming language}
At the moment, the most popular programming
languages used in contests are C++, Python and Java.
2017-05-04 19:23:34 +02:00
For example, in Google Code Jam 2017,
2016-12-28 23:54:51 +01:00
among the best 3,000 participants,
2017-05-04 19:23:34 +02:00
79 \% used C++,
16 \% used Python and
8 \% used Java \cite{goo17}.
2016-12-28 23:54:51 +01:00
Some participants also used several languages.
Many people think that C++ is the best choice
for a competitive programmer,
and C++ is nearly always available in
contest systems.
2017-12-10 11:06:32 +01:00
The benefits of using C++ are that
2016-12-28 23:54:51 +01:00
it is a very efficient language and
its standard library contains a
large collection
of data structures and algorithms.
On the other hand, it is good to
master several languages and understand
2017-01-30 19:39:11 +01:00
their strengths.
2017-02-13 20:42:16 +01:00
For example, if large integers are needed
2016-12-28 23:54:51 +01:00
in the problem,
2017-02-13 20:42:16 +01:00
Python can be a good choice, because it
contains built-in operations for
calculating with large integers.
2017-01-30 19:39:11 +01:00
Still, most problems in programming contests
are set so that
using a specific programming language
is not an unfair advantage.
All example programs in this book are written in C++,
and the standard library's
data structures and algorithms are often used.
The programs follow the C++11 standard,
which can be used in most contests nowadays.
2017-01-30 19:39:11 +01:00
If you cannot program in C++ yet,
now is a good time to start learning.
2016-12-28 23:54:51 +01:00
2017-03-10 13:25:48 +01:00
\subsubsection{C++ code template}
2016-12-28 23:54:51 +01:00
2017-03-10 13:25:48 +01:00
A typical C++ code template for competitive programming
2016-12-28 23:54:51 +01:00
looks like this:
\begin{lstlisting}
#include <bits/stdc++.h>
using namespace std;
int main() {
2016-12-29 01:23:54 +01:00
// solution comes here
2016-12-28 23:54:51 +01:00
}
\end{lstlisting}
The \texttt{\#include} line at the beginning
2017-02-13 20:42:16 +01:00
of the code is a feature of the \texttt{g++} compiler
that allows us to include the entire standard library.
2016-12-28 23:54:51 +01:00
Thus, it is not needed to separately include
libraries such as \texttt{iostream},
\texttt{vector} and \texttt{algorithm},
but rather they are available automatically.
2016-12-28 23:54:51 +01:00
The \texttt{using} line declares
2016-12-29 17:53:58 +01:00
that the classes and functions
of the standard library can be used directly
2016-12-28 23:54:51 +01:00
in the code.
Without the \texttt{using} line we would have
to write, for example, \texttt{std::cout},
2017-02-13 20:42:16 +01:00
but now it suffices to write \texttt{cout}.
2016-12-28 23:54:51 +01:00
The code can be compiled using the following command:
\begin{lstlisting}
2017-05-17 20:23:33 +02:00
g++ -std=c++11 -O2 -Wall test.cpp -o test
2016-12-28 23:54:51 +01:00
\end{lstlisting}
2017-05-17 20:23:33 +02:00
This command produces a binary file \texttt{test}
from the source code \texttt{test.cpp}.
2017-02-13 20:42:16 +01:00
The compiler follows the C++11 standard
2016-12-28 23:54:51 +01:00
(\texttt{-std=c++11}),
optimizes the code (\texttt{-O2})
and shows warnings about possible errors (\texttt{-Wall}).
\section{Input and output}
\index{input and output}
In most contests, standard streams are used for
reading input and writing output.
In C++, the standard streams are
\texttt{cin} for input and \texttt{cout} for output.
In addition, the C functions
\texttt{scanf} and \texttt{printf} can be used.
The input for the program usually consists of
numbers and strings that are separated with
spaces and newlines.
They can be read from the \texttt{cin} stream
as follows:
\begin{lstlisting}
int a, b;
string x;
cin >> a >> b >> x;
\end{lstlisting}
This kind of code always works,
assuming that there is at least one space
2017-02-13 20:42:16 +01:00
or newline between each element in the input.
For example, the above code can read
2017-12-10 11:06:32 +01:00
both of the following inputs:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
2017-01-30 19:39:11 +01:00
123 456 monkey
2016-12-28 23:54:51 +01:00
\end{lstlisting}
\begin{lstlisting}
123 456
2017-01-30 19:39:11 +01:00
monkey
2016-12-28 23:54:51 +01:00
\end{lstlisting}
The \texttt{cout} stream is used for output
as follows:
\begin{lstlisting}
int a = 123, b = 456;
2017-01-30 19:39:11 +01:00
string x = "monkey";
2016-12-28 23:54:51 +01:00
cout << a << " " << b << " " << x << "\n";
\end{lstlisting}
2017-01-30 19:39:11 +01:00
Input and output is sometimes
2016-12-28 23:54:51 +01:00
a bottleneck in the program.
The following lines at the beginning of the code
make input and output more efficient:
\begin{lstlisting}
2017-05-17 20:23:33 +02:00
ios::sync_with_stdio(0);
2016-12-28 23:54:51 +01:00
cin.tie(0);
\end{lstlisting}
Note that the newline \texttt{"\textbackslash n"}
works faster than \texttt{endl},
2017-01-30 19:39:11 +01:00
because \texttt{endl} always causes
2016-12-28 23:54:51 +01:00
a flush operation.
The C functions \texttt{scanf}
and \texttt{printf} are an alternative
to the C++ standard streams.
They are usually a bit faster,
but they are also more difficult to use.
The following code reads two integers from the input:
\begin{lstlisting}
int a, b;
scanf("%d %d", &a, &b);
\end{lstlisting}
The following code prints two integers:
\begin{lstlisting}
int a = 123, b = 456;
printf("%d %d\n", a, b);
\end{lstlisting}
Sometimes the program should read a whole line
from the input, possibly containing spaces.
2017-02-13 20:42:16 +01:00
This can be accomplished by using the
2016-12-28 23:54:51 +01:00
\texttt{getline} function:
\begin{lstlisting}
string s;
getline(cin, s);
\end{lstlisting}
If the amount of data is unknown, the following
2017-02-13 20:42:16 +01:00
loop is useful:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
while (cin >> x) {
2017-01-30 19:39:11 +01:00
// code
2016-12-28 23:54:51 +01:00
}
\end{lstlisting}
This loop reads elements from the input
one after another, until there is no
more data available in the input.
In some contest systems, files are used for
input and output.
An easy solution for this is to write
the code as usual using standard streams,
but add the following lines to the beginning of the code:
\begin{lstlisting}
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
\end{lstlisting}
2017-02-13 20:42:16 +01:00
After this, the program reads the input from the file
2016-12-28 23:54:51 +01:00
''input.txt'' and writes the output to the file
''output.txt''.
2017-02-13 20:42:16 +01:00
\section{Working with numbers}
2016-12-28 23:54:51 +01:00
\index{integer}
\subsubsection{Integers}
2017-01-30 19:39:11 +01:00
The most used integer type in competitive programming
is \texttt{int}, which is a 32-bit type with
a value range of $-2^{31} \ldots 2^{31}-1$
2017-01-30 19:39:11 +01:00
or about $-2 \cdot 10^9 \ldots 2 \cdot 10^9$.
2016-12-28 23:54:51 +01:00
If the type \texttt{int} is not enough,
the 64-bit type \texttt{long long} can be used.
It has a value range of $-2^{63} \ldots 2^{63}-1$
2017-01-30 19:39:11 +01:00
or about $-9 \cdot 10^{18} \ldots 9 \cdot 10^{18}$.
2016-12-28 23:54:51 +01:00
The following code defines a
\texttt{long long} variable:
\begin{lstlisting}
long long x = 123456789123456789LL;
\end{lstlisting}
The suffix \texttt{LL} means that the
type of the number is \texttt{long long}.
A common mistake when using the type \texttt{long long}
2016-12-28 23:54:51 +01:00
is that the type \texttt{int} is still used somewhere
in the code.
For example, the following code contains
a subtle error:
\begin{lstlisting}
int a = 123456789;
long long b = a*a;
cout << b << "\n"; // -1757895751
\end{lstlisting}
Even though the variable \texttt{b} is of type \texttt{long long},
both numbers in the expression \texttt{a*a}
are of type \texttt{int} and the result is
also of type \texttt{int}.
Because of this, the variable \texttt{b} will
contain a wrong result.
The problem can be solved by changing the type
of \texttt{a} to \texttt{long long} or
by changing the expression to \texttt{(long long)a*a}.
2017-01-30 19:39:11 +01:00
Usually contest problems are set so that the
2016-12-28 23:54:51 +01:00
type \texttt{long long} is enough.
Still, it is good to know that
2017-01-30 19:39:11 +01:00
the \texttt{g++} compiler also provides
a 128-bit type \texttt{\_\_int128\_t}
with a value range of
2017-02-25 11:25:34 +01:00
$-2^{127} \ldots 2^{127}-1$ or about $-10^{38} \ldots 10^{38}$.
2016-12-28 23:54:51 +01:00
However, this type is not available in all contest systems.
\subsubsection{Modular arithmetic}
\index{remainder}
\index{modular arithmetic}
We denote by $x \bmod m$ the remainder
when $x$ is divided by $m$.
For example, $17 \bmod 5 = 2$,
because $17 = 3 \cdot 5 + 2$.
2017-02-13 20:42:16 +01:00
Sometimes, the answer to a problem is a
2017-01-30 19:39:11 +01:00
very large number but it is enough to
output it ''modulo $m$'', i.e.,
2016-12-28 23:54:51 +01:00
the remainder when the answer is divided by $m$
(for example, ''modulo $10^9+7$'').
The idea is that even if the actual answer
is very large,
2017-02-13 20:42:16 +01:00
it suffices to use the types
2016-12-28 23:54:51 +01:00
\texttt{int} and \texttt{long long}.
An important property of the remainder is that
in addition, subtraction and multiplication,
2017-02-13 20:42:16 +01:00
the remainder can be taken before the operation:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{rcr}
(a+b) \bmod m & = & (a \bmod m + b \bmod m) \bmod m \\
(a-b) \bmod m & = & (a \bmod m - b \bmod m) \bmod m \\
(a \cdot b) \bmod m & = & (a \bmod m \cdot b \bmod m) \bmod m
\end{array}
\]
2017-02-13 20:42:16 +01:00
Thus, we can take the remainder after every operation
2016-12-28 23:54:51 +01:00
and the numbers will never become too large.
For example, the following code calculates $n!$,
the factorial of $n$, modulo $m$:
\begin{lstlisting}
long long x = 1;
2017-11-19 22:15:52 +01:00
for (int i = 2; i <= n; i++) {
2016-12-28 23:54:51 +01:00
x = (x*i)%m;
}
2017-03-18 11:20:26 +01:00
cout << x%m << "\n";
2016-12-28 23:54:51 +01:00
\end{lstlisting}
2017-05-17 20:23:33 +02:00
Usually we want the remainder to always
2017-01-30 19:39:11 +01:00
be between $0\ldots m-1$.
2016-12-28 23:54:51 +01:00
However, in C++ and other languages,
2017-01-30 19:39:11 +01:00
the remainder of a negative number
is either zero or negative.
2017-02-13 20:42:16 +01:00
An easy way to make sure there
are no negative remainders is to first calculate
2016-12-28 23:54:51 +01:00
the remainder as usual and then add $m$
if the result is negative:
\begin{lstlisting}
x = x%m;
if (x < 0) x += m;
\end{lstlisting}
However, this is only needed when there
are subtractions in the code and the
remainder may become negative.
\subsubsection{Floating point numbers}
\index{floating point number}
The usual floating point types in
competitive programming are
the 64-bit \texttt{double}
and, as an extension in the \texttt{g++} compiler,
the 80-bit \texttt{long double}.
In most cases, \texttt{double} is enough,
but \texttt{long double} is more accurate.
The required precision of the answer
2017-01-30 19:39:11 +01:00
is usually given in the problem statement.
An easy way to output the answer is to use
2016-12-28 23:54:51 +01:00
the \texttt{printf} function
2017-01-30 19:39:11 +01:00
and give the number of decimal places
in the formatting string.
2016-12-28 23:54:51 +01:00
For example, the following code prints
the value of $x$ with 9 decimal places:
\begin{lstlisting}
printf("%.9f\n", x);
\end{lstlisting}
A difficulty when using floating point numbers
is that some numbers cannot be represented
2017-02-13 20:42:16 +01:00
accurately as floating point numbers,
and there will be rounding errors.
2016-12-28 23:54:51 +01:00
For example, the result of the following code
is surprising:
\begin{lstlisting}
double x = 0.3*3+0.1;
printf("%.20f\n", x); // 0.99999999999999988898
\end{lstlisting}
2017-02-13 20:42:16 +01:00
Due to a rounding error,
the value of \texttt{x} is a bit smaller than 1,
2016-12-28 23:54:51 +01:00
while the correct value would be 1.
It is risky to compare floating point numbers
with the \texttt{==} operator,
because it is possible that the values should be
equal but they are not because of precision errors.
2016-12-28 23:54:51 +01:00
A better way to compare floating point numbers
is to assume that two numbers are equal
2017-02-22 20:15:48 +01:00
if the difference between them is less than $\varepsilon$,
2016-12-28 23:54:51 +01:00
where $\varepsilon$ is a small number.
In practice, the numbers can be compared
as follows ($\varepsilon=10^{-9}$):
\begin{lstlisting}
if (abs(a-b) < 1e-9) {
2016-12-29 00:13:40 +01:00
// a and b are equal
2016-12-28 23:54:51 +01:00
}
\end{lstlisting}
Note that while floating point numbers are inaccurate,
integers up to a certain limit can still be
2016-12-28 23:54:51 +01:00
represented accurately.
For example, using \texttt{double},
it is possible to accurately represent all
2017-02-13 20:42:16 +01:00
integers whose absolute value is at most $2^{53}$.
2016-12-28 23:54:51 +01:00
2016-12-29 00:13:40 +01:00
\section{Shortening code}
2016-12-28 23:54:51 +01:00
2016-12-29 00:13:40 +01:00
Short code is ideal in competitive programming,
2017-02-13 20:42:16 +01:00
because programs should be written
2016-12-29 00:13:40 +01:00
as fast as possible.
Because of this, competitive programmers often define
shorter names for datatypes and other parts of code.
2016-12-28 23:54:51 +01:00
2016-12-29 00:13:40 +01:00
\subsubsection{Type names}
2016-12-28 23:54:51 +01:00
\index{tuppdef@\texttt{typedef}}
2016-12-29 00:13:40 +01:00
Using the command \texttt{typedef}
it is possible to give a shorter name
to a datatype.
For example, the name \texttt{long long} is long,
so we can define a shorter name \texttt{ll}:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
typedef long long ll;
\end{lstlisting}
2016-12-29 00:13:40 +01:00
After this, the code
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
long long a = 123456789;
long long b = 987654321;
cout << a*b << "\n";
\end{lstlisting}
2016-12-29 00:13:40 +01:00
can be shortened as follows:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
ll a = 123456789;
ll b = 987654321;
cout << a*b << "\n";
\end{lstlisting}
2016-12-29 00:13:40 +01:00
The command \texttt{typedef}
can also be used with more complex types.
For example, the following code gives
2017-02-13 20:42:16 +01:00
the name \texttt{vi} for a vector of integers
2016-12-29 00:13:40 +01:00
and the name \texttt{pi} for a pair
that contains two integers.
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
typedef vector<int> vi;
typedef pair<int,int> pi;
\end{lstlisting}
2016-12-29 00:13:40 +01:00
\subsubsection{Macros}
\index{macro}
Another way to shorten code is to define
2016-12-29 00:13:40 +01:00
\key{macros}.
A macro means that certain strings in
the code will be changed before the compilation.
In C++, macros are defined using the
\texttt{\#define} keyword.
2016-12-28 23:54:51 +01:00
2016-12-29 00:13:40 +01:00
For example, we can define the following macros:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
#define F first
#define S second
#define PB push_back
#define MP make_pair
\end{lstlisting}
2016-12-29 00:13:40 +01:00
After this, the code
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
v.push_back(make_pair(y1,x1));
v.push_back(make_pair(y2,x2));
int d = v[i].first+v[i].second;
\end{lstlisting}
2016-12-29 00:13:40 +01:00
can be shortened as follows:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
v.PB(MP(y1,x1));
v.PB(MP(y2,x2));
int d = v[i].F+v[i].S;
\end{lstlisting}
2017-01-30 19:39:11 +01:00
A macro can also have parameters
2016-12-29 00:13:40 +01:00
which makes it possible to shorten loops and other
2017-01-30 19:39:11 +01:00
structures.
2016-12-29 00:13:40 +01:00
For example, we can define the following macro:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
#define REP(i,a,b) for (int i = a; i <= b; i++)
\end{lstlisting}
2016-12-29 00:13:40 +01:00
After this, the code
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
for (int i = 1; i <= n; i++) {
2017-01-30 19:39:11 +01:00
search(i);
2016-12-28 23:54:51 +01:00
}
\end{lstlisting}
2016-12-29 00:13:40 +01:00
can be shortened as follows:
2016-12-28 23:54:51 +01:00
\begin{lstlisting}
REP(i,1,n) {
2017-01-30 19:39:11 +01:00
search(i);
2016-12-28 23:54:51 +01:00
}
\end{lstlisting}
Sometimes macros cause bugs that may be difficult
to detect. For example, consider the following macro
that calculates the square of a number:
\begin{lstlisting}
#define SQ(a) a*a
\end{lstlisting}
This macro \emph{does not} always work as expected.
For example, the code
\begin{lstlisting}
cout << SQ(3+3) << "\n";
\end{lstlisting}
corresponds to the code
\begin{lstlisting}
cout << 3+3*3+3 << "\n"; // 15
\end{lstlisting}
A better version of the macro is as follows:
\begin{lstlisting}
#define SQ(a) (a)*(a)
\end{lstlisting}
Now the code
\begin{lstlisting}
cout << SQ(3+3) << "\n";
\end{lstlisting}
corresponds to the code
\begin{lstlisting}
cout << (3+3)*(3+3) << "\n"; // 36
\end{lstlisting}
2016-12-29 00:21:13 +01:00
\section{Mathematics}
Mathematics plays an important role in competitive
programming, and it is not possible to become
2017-02-13 20:42:16 +01:00
a successful competitive programmer without
having good mathematical skills.
This section discusses some important
2016-12-29 00:21:13 +01:00
mathematical concepts and formulas that
are needed later in the book.
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
\subsubsection{Sum formulas}
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
Each sum of the form
2017-02-13 20:42:16 +01:00
\[\sum_{x=1}^n x^k = 1^k+2^k+3^k+\ldots+n^k,\]
2016-12-29 17:53:58 +01:00
where $k$ is a positive integer,
has a closed-form formula that is a
polynomial of degree $k+1$.
2017-04-18 20:39:31 +02:00
For example\footnote{\index{Faulhaber's formula}
There is even a general formula for such sums, called \key{Faulhaber's formula},
but it is too complex to be presented here.},
2016-12-28 23:54:51 +01:00
\[\sum_{x=1}^n x = 1+2+3+\ldots+n = \frac{n(n+1)}{2}\]
2016-12-29 17:53:58 +01:00
and
2016-12-28 23:54:51 +01:00
\[\sum_{x=1}^n x^2 = 1^2+2^2+3^2+\ldots+n^2 = \frac{n(n+1)(2n+1)}{6}.\]
2017-02-13 20:42:16 +01:00
An \key{arithmetic progression} is a \index{arithmetic progression}
sequence of numbers
2016-12-29 17:53:58 +01:00
where the difference between any two consecutive
numbers is constant.
For example,
2017-02-13 20:42:16 +01:00
\[3, 7, 11, 15\]
is an arithmetic progression with constant 4.
The sum of an arithmetic progression can be calculated
2016-12-29 17:53:58 +01:00
using the formula
2017-05-17 20:23:33 +02:00
\[\underbrace{a + \cdots + b}_{n \,\, \textrm{numbers}} = \frac{n(a+b)}{2}\]
2016-12-29 17:53:58 +01:00
where $a$ is the first number,
$b$ is the last number and
$n$ is the amount of numbers.
For example,
2016-12-28 23:54:51 +01:00
\[3+7+11+15=\frac{4 \cdot (3+15)}{2} = 36.\]
2016-12-29 17:53:58 +01:00
The formula is based on the fact
that the sum consists of $n$ numbers and
the value of each number is $(a+b)/2$ on average.
2017-02-13 20:42:16 +01:00
\index{geometric progression}
A \key{geometric progression} is a sequence
of numbers
2016-12-29 17:53:58 +01:00
where the ratio between any two consecutive
numbers is constant.
For example,
2017-02-13 20:42:16 +01:00
\[3,6,12,24\]
is a geometric progression with constant 2.
The sum of a geometric progression can be calculated
2016-12-29 17:53:58 +01:00
using the formula
2017-05-17 20:23:33 +02:00
\[a + ak + ak^2 + \cdots + b = \frac{bk-a}{k-1}\]
2016-12-29 17:53:58 +01:00
where $a$ is the first number,
$b$ is the last number and the
2017-05-17 20:25:50 +02:00
ratio between consecutive numbers is $k$.
2016-12-29 17:53:58 +01:00
For example,
2016-12-28 23:54:51 +01:00
\[3+6+12+24=\frac{24 \cdot 2 - 3}{2-1} = 45.\]
2016-12-29 17:53:58 +01:00
This formula can be derived as follows. Let
2017-05-17 20:23:33 +02:00
\[ S = a + ak + ak^2 + \cdots + b .\]
2017-05-17 20:26:10 +02:00
By multiplying both sides by $k$, we get
2017-05-17 20:23:33 +02:00
\[ kS = ak + ak^2 + ak^3 + \cdots + bk,\]
2016-12-29 17:53:58 +01:00
and solving the equation
2017-05-17 20:23:33 +02:00
\[ kS-S = bk-a\]
2016-12-29 17:53:58 +01:00
yields the formula.
2016-12-28 23:54:51 +01:00
2017-02-13 20:42:16 +01:00
A special case of a sum of a geometric progression is the formula
2016-12-28 23:54:51 +01:00
\[1+2+4+8+\ldots+2^{n-1}=2^n-1.\]
2016-12-29 17:53:58 +01:00
\index{harmonic sum}
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
A \key{harmonic sum} is a sum of the form
2016-12-28 23:54:51 +01:00
\[ \sum_{x=1}^n \frac{1}{x} = 1+\frac{1}{2}+\frac{1}{3}+\ldots+\frac{1}{n}.\]
2017-02-13 20:42:16 +01:00
An upper bound for a harmonic sum is $\log_2(n)+1$.
2017-01-30 19:39:11 +01:00
Namely, we can
modify each term $1/k$ so that $k$ becomes
the nearest power of two that does not exceed $k$.
2016-12-29 17:53:58 +01:00
For example, when $n=6$, we can estimate
the sum as follows:
2016-12-28 23:54:51 +01:00
\[ 1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\frac{1}{5}+\frac{1}{6} \le
1+\frac{1}{2}+\frac{1}{2}+\frac{1}{4}+\frac{1}{4}+\frac{1}{4}.\]
2016-12-29 17:53:58 +01:00
This upper bound consists of $\log_2(n)+1$ parts
($1$, $2 \cdot 1/2$, $4 \cdot 1/4$, etc.),
2017-02-13 20:42:16 +01:00
and the value of each part is at most 1.
2016-12-29 17:53:58 +01:00
\subsubsection{Set theory}
\index{set theory}
\index{set}
\index{intersection}
\index{union}
\index{difference}
\index{subset}
\index{universal set}
\index{complement}
A \key{set} is a collection of elements.
For example, the set
2016-12-28 23:54:51 +01:00
\[X=\{2,4,7\}\]
2016-12-29 17:53:58 +01:00
contains elements 2, 4 and 7.
The symbol $\emptyset$ denotes an empty set,
2017-02-13 20:42:16 +01:00
and $|S|$ denotes the size of a set $S$,
2016-12-29 17:53:58 +01:00
i.e., the number of elements in the set.
For example, in the above set, $|X|=3$.
2017-01-30 19:39:11 +01:00
If a set $S$ contains an element $x$,
2016-12-29 17:53:58 +01:00
we write $x \in S$,
and otherwise we write $x \notin S$.
For example, in the above set
\[4 \in X \hspace{10px}\textrm{and}\hspace{10px} 5 \notin X.\]
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-30 19:39:11 +01:00
New sets can be constructed using set operations:
2016-12-28 23:54:51 +01:00
\begin{itemize}
2016-12-29 17:53:58 +01:00
\item The \key{intersection} $A \cap B$ consists of elements
2017-02-25 11:24:01 +01:00
that are in both $A$ and $B$.
2016-12-29 17:53:58 +01:00
For example, if $A=\{1,2,5\}$ and $B=\{2,4\}$,
then $A \cap B = \{2\}$.
\item The \key{union} $A \cup B$ consists of elements
that are in $A$ or $B$ or both.
For example, if $A=\{3,7\}$ and $B=\{2,3,8\}$,
then $A \cup B = \{2,3,7,8\}$.
\item The \key{complement} $\bar A$ consists of elements
that are not in $A$.
The interpretation of a complement depends on
the \key{universal set}, which contains all possible elements.
2016-12-29 17:53:58 +01:00
For example, if $A=\{1,2,5,7\}$ and the universal set is
2017-01-30 19:39:11 +01:00
$\{1,2,\ldots,10\}$, then $\bar A = \{3,4,6,8,9,10\}$.
2016-12-29 17:53:58 +01:00
\item The \key{difference} $A \setminus B = A \cap \bar B$
consists of elements that are in $A$ but not in $B$.
Note that $B$ can contain elements that are not in $A$.
For example, if $A=\{2,3,7,8\}$ and $B=\{3,5,8\}$,
then $A \setminus B = \{2,7\}$.
2016-12-28 23:54:51 +01:00
\end{itemize}
\end{samepage}
2016-12-29 17:53:58 +01:00
If each element of $A$ also belongs to $S$,
we say that $A$ is a \key{subset} of $S$,
denoted by $A \subset S$.
2017-01-30 19:39:11 +01:00
A set $S$ always has $2^{|S|}$ subsets,
2016-12-29 17:53:58 +01:00
including the empty set.
For example, the subsets of the set $\{2,4,7\}$ are
2016-12-28 23:54:51 +01:00
\begin{center}
$\emptyset$,
2017-01-30 19:39:11 +01:00
$\{2\}$, $\{4\}$, $\{7\}$, $\{2,4\}$, $\{2,7\}$, $\{4,7\}$ and $\{2,4,7\}$.
2016-12-28 23:54:51 +01:00
\end{center}
Some often used sets are
2017-02-13 20:42:16 +01:00
$\mathbb{N}$ (natural numbers),
$\mathbb{Z}$ (integers),
$\mathbb{Q}$ (rational numbers) and
$\mathbb{R}$ (real numbers).
The set $\mathbb{N}$
2016-12-29 17:53:58 +01:00
can be defined in two ways, depending
on the situation:
either $\mathbb{N}=\{0,1,2,\ldots\}$
or $\mathbb{N}=\{1,2,3,...\}$.
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
We can also construct a set using a rule of the form
2016-12-28 23:54:51 +01:00
\[\{f(n) : n \in S\},\]
2016-12-29 17:53:58 +01:00
where $f(n)$ is some function.
2017-01-30 19:39:11 +01:00
This set contains all elements of the form $f(n)$,
2016-12-29 17:53:58 +01:00
where $n$ is an element in $S$.
For example, the set
2016-12-28 23:54:51 +01:00
\[X=\{2n : n \in \mathbb{Z}\}\]
2016-12-29 17:53:58 +01:00
contains all even integers.
\subsubsection{Logic}
\index{logic}
\index{negation}
\index{conjuction}
\index{disjunction}
\index{implication}
\index{equivalence}
The value of a logical expression is either
\key{true} (1) or \key{false} (0).
The most important logical operators are
$\lnot$ (\key{negation}),
$\land$ (\key{conjunction}),
$\lor$ (\key{disjunction}),
$\Rightarrow$ (\key{implication}) and
$\Leftrightarrow$ (\key{equivalence}).
The following table shows the meanings of these operators:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{rr|rrrrrrr}
$A$ & $B$ & $\lnot A$ & $\lnot B$ & $A \land B$ & $A \lor B$ & $A \Rightarrow B$ & $A \Leftrightarrow B$ \\
\hline
0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 \\
0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 \\
1 & 0 & 0 & 1 & 0 & 1 & 0 & 0 \\
1 & 1 & 0 & 0 & 1 & 1 & 1 & 1 \\
\end{tabular}
\end{center}
2017-02-13 20:42:16 +01:00
The expression $\lnot A$ has the opposite value of $A$.
2016-12-29 17:53:58 +01:00
The expression $A \land B$ is true if both $A$ and $B$
are true,
and the expression $A \lor B$ is true if $A$ or $B$ or both
are true.
The expression $A \Rightarrow B$ is true
if whenever $A$ is true, also $B$ is true.
The expression $A \Leftrightarrow B$ is true
if $A$ and $B$ are both true or both false.
\index{predicate}
A \key{predicate} is an expression that is true or false
depending on its parameters.
Predicates are usually denoted by capital letters.
For example, we can define a predicate $P(x)$
that is true exactly when $x$ is a prime number.
Using this definition, $P(7)$ is true but $P(8)$ is false.
\index{quantifier}
A \key{quantifier} connects a logical expression
2017-02-13 20:42:16 +01:00
to the elements of a set.
2016-12-29 17:53:58 +01:00
The most important quantifiers are
$\forall$ (\key{for all}) and $\exists$ (\key{there is}).
For example,
2016-12-28 23:54:51 +01:00
\[\forall x (\exists y (y < x))\]
2016-12-29 17:53:58 +01:00
means that for each element $x$ in the set,
there is an element $y$ in the set
such that $y$ is smaller than $x$.
This is true in the set of integers,
but false in the set of natural numbers.
Using the notation described above,
we can express many kinds of logical propositions.
For example,
2017-05-17 20:23:33 +02:00
\[\forall x ((x>1 \land \lnot P(x)) \Rightarrow (\exists a (\exists b (a > 1 \land b > 1 \land x = ab))))\]
2017-01-30 19:39:11 +01:00
means that if a number $x$ is larger than 1
2016-12-29 17:53:58 +01:00
and not a prime number,
2017-02-13 20:42:16 +01:00
then there are numbers $a$ and $b$
2016-12-29 17:53:58 +01:00
that are larger than $1$ and whose product is $x$.
This proposition is true in the set of integers.
\subsubsection{Functions}
The function $\lfloor x \rfloor$ rounds the number $x$
down to an integer, and the function
$\lceil x \rceil$ rounds the number $x$
up to an integer. For example,
\[ \lfloor 3/2 \rfloor = 1 \hspace{10px} \textrm{and} \hspace{10px} \lceil 3/2 \rceil = 2.\]
The functions $\min(x_1,x_2,\ldots,x_n)$
and $\max(x_1,x_2,\ldots,x_n)$
2017-02-20 22:23:10 +01:00
give the smallest and largest of values
2016-12-29 17:53:58 +01:00
$x_1,x_2,\ldots,x_n$.
For example,
\[ \min(1,2,3)=1 \hspace{10px} \textrm{and} \hspace{10px} \max(1,2,3)=3.\]
\index{factorial}
2017-01-30 19:39:11 +01:00
The \key{factorial} $n!$ can be defined
2016-12-28 23:54:51 +01:00
\[\prod_{x=1}^n x = 1 \cdot 2 \cdot 3 \cdot \ldots \cdot n\]
2016-12-29 17:53:58 +01:00
or recursively
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
0! & = & 1 \\
n! & = & n \cdot (n-1)! \\
\end{array}
\]
2016-12-29 17:53:58 +01:00
\index{Fibonacci number}
2016-12-28 23:54:51 +01:00
2017-02-26 10:21:04 +01:00
The \key{Fibonacci numbers}
%\footnote{Fibonacci (c. 1175--1250) was an Italian mathematician.}
arise in many situations.
2016-12-29 17:53:58 +01:00
They can be defined recursively as follows:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
f(0) & = & 0 \\
f(1) & = & 1 \\
f(n) & = & f(n-1)+f(n-2) \\
\end{array}
\]
2016-12-29 17:53:58 +01:00
The first Fibonacci numbers are
2016-12-28 23:54:51 +01:00
\[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, \ldots\]
2016-12-29 17:53:58 +01:00
There is also a closed-form formula
2017-05-17 20:23:33 +02:00
for calculating Fibonacci numbers, which is sometimes called
\index{Binet's formula} \key{Binet's formula}:
2016-12-28 23:54:51 +01:00
\[f(n)=\frac{(1 + \sqrt{5})^n - (1-\sqrt{5})^n}{2^n \sqrt{5}}.\]
2017-02-20 22:23:10 +01:00
\subsubsection{Logarithms}
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
\index{logarithm}
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
The \key{logarithm} of a number $x$
2017-01-30 19:39:11 +01:00
is denoted $\log_k(x)$, where $k$ is the base
2016-12-29 17:53:58 +01:00
of the logarithm.
2017-01-30 19:39:11 +01:00
According to the definition,
2016-12-29 17:53:58 +01:00
$\log_k(x)=a$ exactly when $k^a=x$.
2016-12-28 23:54:51 +01:00
2017-02-13 20:42:16 +01:00
A useful property of logarithms is
2016-12-29 17:53:58 +01:00
that $\log_k(x)$ equals the number of times
we have to divide $x$ by $k$ before we reach
the number 1.
For example, $\log_2(32)=5$
2017-05-17 20:23:33 +02:00
because 5 divisions by 2 are needed:
2016-12-28 23:54:51 +01:00
\[32 \rightarrow 16 \rightarrow 8 \rightarrow 4 \rightarrow 2 \rightarrow 1 \]
2017-02-13 20:42:16 +01:00
Logarithms are often used in the analysis of
2017-01-30 19:39:11 +01:00
algorithms, because many efficient algorithms
2017-02-13 20:42:16 +01:00
halve something at each step.
2017-01-30 19:39:11 +01:00
Hence, we can estimate the efficiency of such algorithms
using logarithms.
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
The logarithm of a product is
2016-12-28 23:54:51 +01:00
\[\log_k(ab) = \log_k(a)+\log_k(b),\]
2016-12-29 17:53:58 +01:00
and consequently,
2016-12-28 23:54:51 +01:00
\[\log_k(x^n) = n \cdot \log_k(x).\]
2016-12-29 17:53:58 +01:00
In addition, the logarithm of a quotient is
2016-12-28 23:54:51 +01:00
\[\log_k\Big(\frac{a}{b}\Big) = \log_k(a)-\log_k(b).\]
2016-12-29 17:53:58 +01:00
Another useful formula is
2016-12-28 23:54:51 +01:00
\[\log_u(x) = \frac{\log_k(x)}{\log_k(u)},\]
2016-12-29 17:53:58 +01:00
and using this, it is possible to calculate
logarithms to any base if there is a way to
calculate logarithms to some fixed base.
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
\index{natural logarithm}
2016-12-28 23:54:51 +01:00
2016-12-29 17:53:58 +01:00
The \key{natural logarithm} $\ln(x)$ of a number $x$
2017-04-17 11:18:29 +02:00
is a logarithm whose base is $e \approx 2.71828$.
2017-01-30 19:39:11 +01:00
Another property of logarithms is that
2017-02-13 20:42:16 +01:00
the number of digits of an integer $x$ in base $b$ is
2016-12-29 17:53:58 +01:00
$\lfloor \log_b(x)+1 \rfloor$.
For example, the representation of
2017-02-13 20:42:16 +01:00
$123$ in base $2$ is 1111011 and
2016-12-28 23:54:51 +01:00
$\lfloor \log_2(123)+1 \rfloor = 7$.
\section{Contests and resources}
2017-02-21 18:25:37 +01:00
\subsubsection{IOI}
2017-02-27 20:29:32 +01:00
The International Olympiad in Informatics (IOI)
is an annual programming contest for
2017-02-21 18:25:37 +01:00
secondary school students.
Each country is allowed to send a team of
four students to the contest.
There are usually about 300 participants
2017-02-22 22:22:25 +01:00
from 80 countries.
2017-02-21 18:25:37 +01:00
The IOI consists of two five-hour long contests.
In both contests, the participants are asked to
solve three algorithm tasks of various difficulty.
The tasks are divided into subtasks,
each of which has an assigned score.
Even if the contestants are divided into teams,
they compete as individuals.
2017-02-22 22:22:25 +01:00
The IOI syllabus \cite{iois} regulates the topics
2017-02-21 18:25:37 +01:00
that may appear in IOI tasks.
2017-04-18 20:34:34 +02:00
Almost all the topics in the IOI syllabus
are covered by this book.
2017-02-21 18:25:37 +01:00
Participants for the IOI are selected through
2017-02-21 18:25:37 +01:00
national contests.
Before the IOI, many regional contests are organized,
such as the Baltic Olympiad in Informatics (BOI),
the Central European Olympiad in Informatics (CEOI)
and the Asia-Pacific Informatics Olympiad (APIO).
Some countries organize online practice contests
for future IOI participants,
2017-04-18 20:34:34 +02:00
such as the Croatian Open Competition in Informatics \cite{coci}
and the USA Computing Olympiad \cite{usaco}.
2017-05-17 20:23:33 +02:00
In addition, a large collection of problems from Polish contests
is available online \cite{main}.
2017-02-21 18:25:37 +01:00
\subsubsection{ICPC}
2017-02-22 22:22:25 +01:00
The International Collegiate Programming Contest (ICPC)
2017-02-21 18:25:37 +01:00
is an annual programming contest for university students.
Each team in the contest consists of three students,
and unlike in the IOI, the students work together;
there is only one computer available for each team.
2017-02-21 18:25:37 +01:00
The ICPC consists of several stages, and finally the
best teams are invited to the World Finals.
While there are tens of thousands of participants
in the contest, there are only a small number\footnote{The exact number of final
2017-05-25 22:38:25 +02:00
slots varies from year to year; in 2017, there were 133 final slots.} of final slots available,
2017-02-21 18:25:37 +01:00
so even advancing to the finals
is a great achievement in some regions.
In each ICPC contest, the teams have five hours of time to
solve about ten algorithm problems.
2017-02-21 18:25:37 +01:00
A solution to a problem is accepted only if it solves
all test cases efficiently.
During the contest, competitors may view the results of other teams,
2017-02-21 18:25:37 +01:00
but for the last hour the scoreboard is frozen and it
is not possible to see the results of the last submissions.
The topics that may appear at the ICPC are not so well
specified as those at the IOI.
In any case, it is clear that more knowledge is needed
at the ICPC, especially more mathematical skills.
\subsubsection{Online contests}
There are also many online contests that are open for everybody.
At the moment, the most active contest site is Codeforces,
which organizes contests about weekly.
2017-02-21 18:25:37 +01:00
In Codeforces, participants are divided into two divisions:
beginners compete in Div2 and more experienced programmers in Div1.
Other contest sites include AtCoder, CS Academy, HackerRank and Topcoder.
Some companies organize online contests with onsite finals.
Examples of such contests are Facebook Hacker Cup,
Google Code Jam and Yandex.Algorithm.
Of course, companies also use those contests for recruiting:
performing well in a contest is a good way to prove one's skills.
\subsubsection{Books}
2017-02-21 19:10:42 +01:00
There are already some books (besides this book) that
2017-05-17 20:23:33 +02:00
focus on competitive programming and algorithmic problem solving:
2017-02-21 19:10:42 +01:00
\begin{itemize}
\item S. S. Skiena and M. A. Revilla:
2017-02-26 12:51:38 +01:00
\emph{Programming Challenges: The Programming Contest Training Manual} \cite{ski03}
2017-05-17 20:23:33 +02:00
\item S. Halim and F. Halim:
\emph{Competitive Programming 3: The New Lower Bound of Programming Contests} \cite{hal13}
2017-02-26 12:51:38 +01:00
\item K. Diks et al.: \emph{Looking for a Challenge? The Ultimate Problem Set from
the University of Warsaw Programming Competitions} \cite{dik12}
2017-02-21 19:10:42 +01:00
\end{itemize}
The first two books are intended for beginners,
whereas the last book contains advanced material.
Of course, general algorithm books are also suitable for
competitive programmers.
2017-02-27 20:29:32 +01:00
Some popular books are:
2017-02-21 19:10:42 +01:00
\begin{itemize}
\item T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein:
2017-02-26 12:51:38 +01:00
\emph{Introduction to Algorithms} \cite{cor09}
2017-02-21 19:10:42 +01:00
\item J. Kleinberg and É. Tardos:
2017-02-26 12:51:38 +01:00
\emph{Algorithm Design} \cite{kle05}
2017-02-21 19:10:42 +01:00
\item S. S. Skiena:
2017-02-26 12:51:38 +01:00
\emph{The Algorithm Design Manual} \cite{ski08}
2017-02-21 19:10:42 +01:00
\end{itemize}