Editorial for COCI '10 Contest 1 #3 Sretan


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

A naïve solution would iterate over positive integers and check their luck until the K^\text{th} lucky integer is found. Such a solution is worth 20\% of points.

A better solution is possible if a pattern is noticed in lucky numbers.

Specifically, if we substitute the digit 4 with 0, and 7 with 1, the lucky numbers correspond to binary number notation, with an additional quirk that leading zeros are allowed.

A solution iteratively generating the first K lucky numbers using this observation has a complexity of \mathcal O(K) and is worth 50\% of points.

Another speedup can be made by observing the number of lucky numbers with a given length. There are 2^N lucky numbers with length N (since each of N positions can contain one of two digits).

Knowing this, we can determine the length L of the required lucky number and the index P of that number among all lucky numbers of length L. Then, it is sufficient to output the number P-1 in binary with the appropriate number of leading zeros, substituting digits 0 and 1 with 4 and 7, respectively.


Comments

There are no comments at the moment.