Editorial for Baltic OI '14 P3 - Sequence


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.

Subtask 1

We try all possible N values (starting from smallest - 1). With each N, we check if it is the answer using brute force - checking if all sequence numbers contain the required digits.

Time complexity: O(NK)

N - correct answer.

Since K1000 and N1000 in the first subtask, this solution is enough for it.

Subtask 2

If the full solution is programmed inefficiently, it is possible to acquire an O(K2) solution.

Time complexity: O(K2)

Subtask 3

We are assuming that all elements of the given sequence are equal. Let's denote this digit as d.

If 1d8, then our answer is N=d×10x=d00.

If d=9, then N=889 (some number of digits 8 and one digit 9 at the end).

If d=0, then N=10x=100.

The length of N depends on K. For example, if K=5 and our sequence is 3 3 3 3 3, then d=3 and N=d00=300=30. If K=11 and d=3, then N=d00=300=300.

Time complexity: O(1)

Subtask 4

We'll solve our task by solving a more complex task first: for each ith sequence element we'll declare sequence Ai. Ai is the sequence of digits which ith sequence element has to have. For our initial task, Ai={di}, where di - ... (DMOJ editor note: did the author get lazy here?)

Let's denote N=(X)y, where y=Nmod10 is the last digit and X=N10. Now we have to try all possible y values (0,1,,9). After we have locked y with one of the possible values, our sequence looks like this:

(X)y,(X)y+1,,(X)8,(X)9,(X+1)0,(X+1)1,,(X+1)8,(X+1)9,(X+2)0,

After removing last digit, we get sequence X,,X,X+1,,X+1,X+2,

What digits does X have to have? (X)y has A1, so X must have A1{y}. (X)(y+1) has A2, so X must have A2{y+1} and so on.

By merging these requirements, we can get a new sequence of sets B1,B2,. B1 declares the required digits for X, B2 - for X+1, and so on. We repeat the same steps with our new sequence. What happens when we repeat these steps? We started with sequence length K, so after the first step, our new sequence length will be not bigger than K10+1. So after logK steps our sequence will be of length 1 or 2 - at this step, we can produce the answer.

Time complexity: O(KlogK)


Comments

There are no comments at the moment.