Editorial for Baltic OI '08 P3 - Magical Stones


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.

The task is, described alternatively, to find the ith word in the lexicographical order, of length n, composed of letters I and X, satisfying two conditions:

  1. letters I and X are next to each other at at most k positions,
  2. the word read backwards is not lexicographically less than the original word.

From now on, we will call each word satisfying the above conditions a correct word. Every two neighbouring positions of a word which contain different letters are called a letter change.

We will find the letters of the ith word one by one, from left to right. We start with an empty prefix of the word, which we will call ϵ. In the mth step of the algorithm (starting from m=0), knowing a prefix a1am of the requested word, our task is to find its next letter. In order to perform this operation, we compute the number of correct words that start with the prefix a1amI; if i is not greater than this number then — thanks to the lexicographical ordering of correct words — the (m+1)st letter of the requested word is I. Otherwise, it is X.

To help us precisely formulate the described idea of the solution, let us introduce the following notation: σ(a1am) denotes the number of correct words that start with the prefix a1am.

With those notations, model solution can described by the following simple pseudocode:

Copy
prefix := ε;
j := i;
for m := 0 to n-1 do
  if j <= σ(prefix + I) then
    prefix := prefix + I;
  else
    prefix := prefix + X;
    j := j - σ(prefix + I);
if j > 1 then
  return NO SUCH STONE;
else
  return prefix;

The case when the correct result is NO SUCH STONE requires some explanation. This happens only if i>σ(ϵ). In such case, the final value of prefix in the above algorithm will be XXX...X and j will be greater than 1. However, we notice that it is sufficient to check the latter condition, because it can be satisfied if and only if i>σ(ϵ).

How to Compute Values of σ

Firstly let us introduce one more notation: σ(a1am,blb1) denotes the number of correct words that start with the prefix a1am and end with the suffix blb1. If m+l>n then the prefix and suffix overlap. Each correct word that starts with the prefix a1am is of exactly one of the following forms:

  • l final letters of the word (where 0l<m) read backwards are exactly the same as l starting letters of the word, I is the (l+1)st letter from the beginning of the word and X is the (l+1)st letter from the end of the word.
  • m final letters of the word read backwards are the same as m starting letters of the word (read from left to right). This leads directly to the following formula:

σ(a1am)=l=0m1([al+1=I]σ(a1am,Xala1))+σ(a1am,ama1)(1)

where [al+1=I] is equal to 1 if al+1= I and 0 otherwise. In general, expression [condition], where condition is a logical statement, has a value of 1 if the condition is true and 0 otherwise.

We now need to show how to compute the summands from the above equation. Note that we can assume that am= I, as the pseudocode only needs to know the value of σ(prefixI). Let us assume that there are exactly km letter changes in the word a1am and kl letter changes in the word Xala1.

Lemma 1. If 2mn (this simply implies that prefix a1am and suffix ama1 do not overlap) then:

σ(a1am,ama1)=12i=0(k2km)/2(n2m+12i)+12i=0(k2km)/2((n2m+1)/2i)

Proof: The total number of words of the form a1amama1 over the alphabet {I,X} that satisfy condition 1 (i.e. contain at most k letter changes) equals:

S=i=0(k2km)/2(n2m+12i)

This holds, because there are n2m free positions in the middle of the word, so a letter change could take place at any of the n2m+1 pairs of consecutive positions. This explains the binomial coefficients in the formula. We also know that the number of letter changes in the middle part of the word is not greater than k2km and even, as the middle part of the word is bounded by a letter am. This explains why the formula contains a sum over {0,,(k2km)/2}.

The number of palindromic words of the form a1amama1 that satisfy condition 1 is:

Sp=i=0(k2km)/2((n2m+1)/2i)

This formula holds for even n, because one half of the word can be filled arbitrarily using no more than (k2km)/2 letter changes. The remaining part of the word is uniquely determined and contains the same number of letter changes. If n is odd, the leading n/2 letters define n/2 trailing letters, as well as the middle letter, as only one letter can be inserted in the middle of the word without adding new letter changes.

The number of words of the form a1amama1 that satisfy condition 1 and are not palindromes is SSp. Exactly one half of such words are correct, as if we reverse a correct word w it becomes incorrect and non-palindromic. On the other hand, all palindromic words of the form a1amama1 are correct. This gives us the following formula for the total number of correct words of the form a1amama1:

12(SSp)+Sp=12S+12Sp

which is equivalent to the formula from Lemma 1. ■

Lemma 2. If m+l+1n and al+1= I, am= I then:

σ(a1am,Xala1)=i=0(kkmkl1)/2(nml2i+1)

Proof: Here we can choose letter changes from nml positions, knowing that the total number of letter changes is odd (because am= I X) and not greater than kkmkl. Notice that the condition al+1= I already implies that every possible choice of middle letters of the word a1amXala1 leads to a correct word — this is the reason why the formula from Lemma 2 is much simpler than the previous one (from Lemma 1). ■

Finally, if the prefix and the suffix overlap (2m>n or m+l+1>n) then the number of correct words with the desired prefix and suffix is equal to 0 or 1 and this number can be computed in a straightforward manner. This, together with formulas from Lemmas 1 and 2, concludes the algorithm for computing the values of the σ function.

Time Complexity

Let us analyse the time complexity of the whole algorithm. In the pseudocode, values of the σ function are computed O(n) times (line 4). Every such computation is done using Formula (1) and requires O(n) computations of values of σ(prefix,suffix). Finally every such value can be computed using Lemma 1, Lemma 2 or direct computation if prefix and suffix overlap — each of these requires O(n) operations, provided that all values of binomial coefficients (ab) for 0a,bn are precomputed. This preprocessing can be done in O(n2) time complexity with a well-known formula:

(ab)=(a1b1)+(a1b) for a>b>0

and simple formulas for border cases like a<b, or a=b, or b=0. The total time complexity is therefore O(nnn)+O(n2)=O(n3). The upper limit for n in our task is equal to 60, so this solution is fast enough to receive perfect score.


Comments

There are no comments at the moment.