L2: Divide-and-Conquer
In this lab we consider the problems from Jutge.org, section EDA \rightarrow Divide and Conquer. The problems in this section are of three different types
- variations on dichotomic search,
- efficient exponentiation, and
- few others that don’t fit with a pattern.
Variations on Dichotomic Search
There is a first line of exercises that can be thought as variations on Dichotomic Search, and some of them we saw already in the problem class P2. They are the following:
- P81966 Dichotomic search
- P84219 First occurrence 🔶
- P33412 Resistant dichotomic search 🔶 (similar to Problem 2.12 from problem class P3)
- X82938 Search in a unimodal vector (similar to Problem 2.9 from problem class P3)
Once you have chosen a problem to solve, download the .zip archive of the problem and unzip
it. In the obtained folder there are:
- a file .pdf with the official problem statement
- one or more text files with examples of input
sample.inp
- the corresponding correct outputs
sample.cor
After creating a C++ source file to solve the problem, compile it with g++ -std=c++11
.
Not using the flag -std=c++11
.
Possible useful flags are -Wall -Wextra -Wno-overflow -Wpedantic -Werror -D_GLIBCXX_DEBUG
Test the executable file obtained on the given example(s), for instance with ./a.out < sample.inp > output
. Also, test your code against simple/trivial inputs, e.g. a graph with no-edges.
Writing the input by hand.
Not checking the exact formatting of the output.
To see whether the output
is the same as sample.cor
a way is to use diff output sample.cor
Once the code compiles and on the given samples it gives the correct output submit it to the Jutge for automatic evaluation.
Sending to the Jutge code that was not even compiled locally.
Not checking if the output was correct on the given inputs.
You are “stuck” on a problem if you spent around 1 hour trying to solve it but still the Jutge is rejecting your solution.
Possible approaches to getting “un-stuck” are:
do not think about that problem for some time and come back to your code with fresh eyes
look again at the theory
try to explain your code to another student
ask for hints, e.g. by email. Be specific on the type of help you need. Include in the email the code of your approach.
From this block of exercises, in class we solve together the ones marked with 🔶. They are the same as the one listed below.1
Efficient exponentiation
A second line of exercises are about efficient exponentiation (in one way or another). They are the following.
- P29212 Modular exponentiation 🔶 (similar to Problem 1.23 from problem class P2)
- P61833 Powers of a matrix 🔶
- P74219 Fibonacci numbers (2)
- X39049 Powers of permutations 🔶
The Fibonacci sequence (F_n)_{n\in \mathbb N} is defined as follows: F_0=0, F_1=1 and for every n\geq 2, F_n=F_{n-1}+F_{n-2}. That is \begin{pmatrix} F_n\\ F_{n-1} \end{pmatrix} = \begin{pmatrix} 1 &1\\ 1& 0 \end{pmatrix} \cdot \begin{pmatrix} F_{n-1}\\ F_{n-2} \end{pmatrix} = \begin{pmatrix} 1 &1\\ 1& 0 \end{pmatrix} ^2 \cdot \begin{pmatrix} F_{n-2}\\ F_{n-3} \end{pmatrix} =\cdots = \begin{pmatrix} 1 &1\\ 1& 0 \end{pmatrix} ^{n-1} \cdot \begin{pmatrix} F_{1}\\ F_{0} \end{pmatrix} \ .
It is not hard to see (by induction on n), that \begin{pmatrix} 1 &1\\ 1& 0 \end{pmatrix} ^{n-1} = \begin{pmatrix} F_n &F_{n-1}\\ F_{n-1}& F_{n-2} \end{pmatrix} for each n\geq 2.
Once you have chosen a problem to solve, download the .zip archive of the problem and unzip
it. In the obtained folder there are:
- a file .pdf with the official problem statement
- one or more text files with examples of input
sample.inp
- the corresponding correct outputs
sample.cor
After creating a C++ source file to solve the problem, compile it with g++ -std=c++11
.
Not using the flag -std=c++11
.
Possible useful flags are -Wall -Wextra -Wno-overflow -Wpedantic -Werror -D_GLIBCXX_DEBUG
Test the executable file obtained on the given example(s), for instance with ./a.out < sample.inp > output
. Also, test your code against simple/trivial inputs, e.g. a graph with no-edges.
Writing the input by hand.
Not checking the exact formatting of the output.
To see whether the output
is the same as sample.cor
a way is to use diff output sample.cor
Once the code compiles and on the given samples it gives the correct output submit it to the Jutge for automatic evaluation.
Sending to the Jutge code that was not even compiled locally.
Not checking if the output was correct on the given inputs.
You are “stuck” on a problem if you spent around 1 hour trying to solve it but still the Jutge is rejecting your solution.
Possible approaches to getting “un-stuck” are:
do not think about that problem for some time and come back to your code with fresh eyes
look again at the theory
try to explain your code to another student
ask for hints, e.g. by email. Be specific on the type of help you need. Include in the email the code of your approach.
From this block of exercises, in class we solve together the ones marked with 🔶. They are the same as the one listed below.2
Wildcards
A third type of exercises don’t really share much similarities (apart from being amenable to a divide-and-conquer approach). They are the following.
- P80595 How many inversions? (similar to Problem 2.11 from problem class P3)
- P58512 Interest rates ❗
- P34682 Fixed points 🔶 ❗
Once you have chosen a problem to solve, download the .zip archive of the problem and unzip
it. In the obtained folder there are:
- a file .pdf with the official problem statement
- one or more text files with examples of input
sample.inp
- the corresponding correct outputs
sample.cor
After creating a C++ source file to solve the problem, compile it with g++ -std=c++11
.
Not using the flag -std=c++11
.
Possible useful flags are -Wall -Wextra -Wno-overflow -Wpedantic -Werror -D_GLIBCXX_DEBUG
Test the executable file obtained on the given example(s), for instance with ./a.out < sample.inp > output
. Also, test your code against simple/trivial inputs, e.g. a graph with no-edges.
Writing the input by hand.
Not checking the exact formatting of the output.
To see whether the output
is the same as sample.cor
a way is to use diff output sample.cor
Once the code compiles and on the given samples it gives the correct output submit it to the Jutge for automatic evaluation.
Sending to the Jutge code that was not even compiled locally.
Not checking if the output was correct on the given inputs.
You are “stuck” on a problem if you spent around 1 hour trying to solve it but still the Jutge is rejecting your solution.
Possible approaches to getting “un-stuck” are:
do not think about that problem for some time and come back to your code with fresh eyes
look again at the theory
try to explain your code to another student
ask for hints, e.g. by email. Be specific on the type of help you need. Include in the email the code of your approach.
From this block of exercises, in class we solve together the ones marked with 🔶. They are the same as the one listed below.3
Remember that for any doubt on the exercises you can ask me by email too until 6 days before the lab exam.