Editorial for COCI '23 Contest 5 #3 Piratski Kod
Submitting an official solution before solving the problem yourself is a bannable offence.
For 20 points it was enough to carefully implement a brute force algorithm calculating values of all pirate codes.
Let's introduce some notation:
- number of pirate entries of length
.
- sum of values of all pirate entries of length
.
- sum of values of all pirate codes of length
.
We can easily notice that . It only remains to determine
and
.
Number of pirate entries of length is
because we see that from a pirate entry of length
, by changing the last digit to
and concatenating two ones at the end we get a pirate entry of
length
that ends with
. From a pirate entry of length
, by changing second to last digit to
and concatenating a single
at the end, we get a pirate entry of length
that ends with
. From this
analysis we get a recursion of Fibonacci numbers and we can easily check the basis for
.
Pirate entries of length represent numbers from
to
so the following holds:
Now we can finish the task with dynamic programming in time complexity .
Comments