- The sequence of integers
$S_n$ called Schröder models the following combinatorial phenomenon: In a grid of size n × n, how many ways do I have to walk from the lower left corner to the upper right corner without ever crossing the diagonal(0, 0) - (n, n)
and using exclusively the paths up (↑), to the right (→) and in diagonal mount to the right (↗).
To calculate
or
For the
- It seems difficult to believe that they calculate the same sequence!
- They don't even seem to have the same complexity. By complexity, we mean the number of calls to S. So, calling S0 or S1 in the two definitions has a cost of 1. However, calling S3 with the first definition does not seem to have the same cost as with the second definition!
- The surprise does not end here!
- It is also said that the sequence is diabolical, that its complexity is monumental and that it calculates values quickly enormous.
- To us, it is a half-truth! We affirm that the sequence can be calculated in linear time even if, in fact, the calculated values tend to the enormous.
- In this exercise, we propose to validate or invalidate these statements experimentally.
- If we wait for a direct implementation, faithful to the definitions of the sequence for the first part of the exercise, we expect an optimized implementation for the second part of the exercise.
- The second part of the exercise needs a careful implementation of the sequence. First, it is necessary a clearly efficient implementation, as referred to in the previous point. Second, because it considers larger input values, the sequence will very quickly return values that clearly exceed the size of the integers. For this, it is advisable to use arbitrary precision arithmetic. Such functionality can be found in the zarith library.
- One line with two integers
a
andb
separated by a space.
- A first line with two integers
u
andv
separated by a space. The integeru
is the result of$S_a$ by the first definition of the Schröder sequence. The integerv
is the number of times the recursive function that faithfully implements the first definition of$S$ is called. - A second line with two integers
x
andy
separated by a space. The integerx
is the result of$S_a$ by the second definition of the Schröder sequence. The integery
is the number of times the recursive function that faithfully implements the second definition of$S$ is called. - A third line with an integer
z
. This integer is$S_b$ .
6 100
1806 70
1806 25
28747611153504860266534250007458881388313583561117443629896620307440340890
- For the first integer of the input, 6, the value of
$S_6$ was calculated with the first definition of the sequence. The result was 1806, and it was necessary to execute 70 calls to the function. - In the second line, the same results are presented, but using the second definition of the sequence. Which naturally gives the same value for the term
$S_6$ , 1806. But the number of calls to the function is much more interesting, here 25. - The third line presents the value of
$S_{100}$ .
cd pbA/
dune exec pbA < input