Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

CROWD - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

DIFFICULTY

SIMPLE

PREREQUISITES

Simple Math, Repeated Squaring

PROBLEM

How many sequences of 0s and 1s of length N are there such that there are at least 3 consecutive 1s.

QUICK EXPLANATION

You can see that the original problem statement is equivalent to the the above by saying that among N houses, you invite only those houses for which you put a 1 in the sequence.

Now, the number of ways you can make a sequence of 0s and 1s of length N, such that there are no more than two consecutive 1s is
f(N) = f(N-1) + f(N-2) + f(N-3)

This is known as the Tribonacci Numbers.

The answer to the problem statement would then be 2N - f(N).

Since N can be very large, 2N can be calculated by repeated squaring and f(N) can be calculated by matrix exponentiation.

EXPLANATION

First, let us see what insights are used to derive the formula.

The number of sequences of N digits where each digit is either 0 or 1 is 2N.

We wish to find the number of sequences such that there are no more than 2 consecutive 1s.

Lemma:

The entire world of such sequences can be constructed by concatenating strings taken from the following prefix-free set
S = { 0, 10, 110 }

Proof:

Let us call a sequence that contains no more than two consecutive 1s as a valid string.

(1) ... All strings with less than or equal to 2 letters are valid.
(2) ... Any substring of a valid string is also a valid string.
(3) ... From (2), any suffix of a valid string is also a valid string.
(4) ... For a string T, constructed by taking strings from S, T should also partition into S.
(5) ... If T cannot partition into S, it cannot be constructed by taking strings from S.
(6) ... Since S is prefix-free, every string has a unique partitioning into S, or none at all.

For example,
0101100100010110 can be partitioned into S as
0101100100010110
And this is the only valid partitioning into S.

(7) ... For a valid string A, A always has a prefix that matches a string in S.

Let us consider all possible 3 letter prefixes of a string

000x.. matches 0 from S (remaining string 00x..)
001x.. matches 0 from S (remaining string 01x..)
010x.. matches 0 from S (remaining string 10x..)
011x.. matches 0 from S (remaining string 11x..)
100x.. matches 10 from S (remaining string 0x..)
101x.. matches 10 from S (remaining string 1x..)
110x.. matches 110 from S (remaining string x..)
111x.. Does not match any string in S

But, if A contains the prefix 111 then A is not a valid string.

Hence, for any valid string A, some prefix of A will always be one of the strings in S.

(8) ... From (3) and (7), a valid string A will always have a partitioning into S.
(9) ... We can see that concatenating strings from S will always generate a valid string.

From (8) and (9) we can say that taking strings from S will generate all possible valid strings.

Now, the number of valid strings of length N will be f(N), which is equal to

  • f(N-1), after choosing "0" from S as prefix, all possible valid strings of length N-1 can be used
  • f(N-2), after choosing "10" from S as prefix, all possible valid strings of length N-2 can be used
  • f(N-3), after choosing "110" from S as prefix, all possible valid strings of length N-3 can be used

Thus,
f(N) = f(N-1) + f(N-2) + f(N-3)

We have to find the result modulo 1000000007.

Now, since N can be quite large, we cannot even iterate for O(N) complexity and find the result. We can build the following matrix representation from the above formula

                                              |1 1 0|
[f(N+1) f(N) f(N-1)] = [f(N) f(N-1) f(N-2)] * |1 0 1|
                                              |1 0 0|

Now we see that multiplying the 3x3 matrix above (let us call it M) k times will give us f(N+k).

We can consider the base case [f(2) f(1) f(0)] = [4, 2, 1] and find MN-2. Once we have MN-2, we can find the answer by multiplying MN-2 to [4, 2, 1], and taking the first value.

You can find MN-2 by repeated squaring on the matrix. Repeated Squaring (and similarly Matrix Exponentiation) is a very useful technique that you should add to your problem solving arsenal.

RELATED PROBLEMS

CSUMD
Go through the Related Problems section of the above as well for more problems :-)

SETTERS SOLUTION

Can be found here

TESTERS SOLUTION

Can be found here


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>