PROBLEM LINK:DIFFICULTYSIMPLE PREREQUISITESSimple Math, Repeated Squaring PROBLEMHow many sequences of 0s and 1s of length N are there such that there are at least 3 consecutive 1s. QUICK EXPLANATIONYou 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 This is known as the Tribonacci Numbers. The answer to the problem statement would then be Since N can be very large, EXPLANATIONFirst, 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 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 prefixfree set Proof: Let us call a sequence that contains no more than two consecutive 1s as a valid string.
For example,
Let us consider all possible 3 letter prefixes of a string
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.
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
Thus, 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(N1)] = [f(N) f(N1) f(N2)] * 1 0 1 1 0 0 Now we see that multiplying the We can consider the base case You can find M^{N2} 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 PROBLEMSCSUMD SETTERS SOLUTIONCan be found here TESTERS SOLUTIONCan be found here
This question is marked "community wiki".
asked 11 Sep '12, 15:15
showing 5 of 11
show all

Maybe while explaining you forgot what the actual question asked was! The answer would instead be ( 2^{n}  f(n) ) % (10^{9}+7) which is equal to the number of ways in which Chef can go wrong. answered 12 Sep '12, 10:54

the recurrence i used was f(n)=3f(n1)f(n2)f(n3)2f(n4) answered 11 Sep '12, 21:02

10101 is a valid string but how can it be partitioned into S? answered 29 Sep '12, 19:30
1
@abhi92 Good catch :) The partition into S only gives strings that have a 0 at the end. Using arguments from the editorial one would be led to believe f(1) = 1. But by setting f(1) = 2. We are ensuring that we calculate all the valid strings that end in 1.
(30 Sep '12, 01:25)

I used the same recursion and same Matrix exponentiation technique but still now use... :( Used to get TLE. :O answered 11 Sep '12, 21:39

Can someone suggest me why my code gives TLE while the problem setters code doesnt ? answered 11 Sep '12, 21:47

Very powerful tutorial! Big ups to Vamsi. The tutorial was well laid out. Thanks. answered 13 Sep '12, 07:02

Hi , I think the problem like this out of N numbers you have to select 0 or 1 or 2 consecutive one 0 consecutive one 1 ways 1 consecutive one N ways 2 consecutive one N1 ways than answer is 2^N  2N but i got wrong answer.
Also i am not getting
answered 14 Sep '12, 12:42

@admin..my code is still giving TLE http://www.codechef.com/viewsolution/1371826 can anybody plzz help??
link
This answer is marked "community wiki".
answered 24 Sep '12, 16:18

@author : how can you say f[n] = f[n1]+f[n2]+f[n3] there f[0] = 0 f[1] = 0 f[2] = 0 f[3] = 1 then for n = 4 2^4 is 16 and f[4] = 1 then ans is ans = 2^41 ans = 15 am i right or not answered 01 Jun '13, 18:23

can anyone explain the solution for test cases 5,6,7,8,9,10....i m not getting the logic behind it... it seems that this can be solved via normal mathematics but maybe i m getting it wrong.. Thanks in advance....(y) answered 29 Sep '14, 22:36

see http://fusharblog.com/solvinglinearrecurrenceforprogrammingcontest/ to Matrix Exponentiation :D answered 31 Jan '16, 23:45

I was a bit focused on deriving some recursive relationship involving only 2 terms for most of the time... After a while, I realized that maybe 3 terms should try to be used instead but couldn't do anything about it... This editorial just makes everything very clear and it is, as usual, very well written :D
Great work, Bruno
can anybody explain how to find the matrix ? like i want to find the matrix for f(n)=f(n1)+f(n2)+2*f(n3)c, where c is a constant. is there a method to find it or just hit and trial?
I figured out that it was the Tribonacci Numbers (I didn't know that's what they were called at the time), but couldn't figure out a formula to find f(N) without actually solving for F(N1), F(N2), and F(N3). I see now that I needed Matrix Exponentiation to find the formula. Unfortunately, I still don't quite understand it, but hopefully with some reading and practice it will make sense.
Dealing with such a constant is a bit of an extension, but the basic strategy is: suppose we have f(n1), f(n2), and f(n3), then our matrix should give us the same but with n+1 [so f(n), f(n1) and f(n2)]. The equations for f(n1) and f(n2) are trivial, but to handle adding the constant for f(n) we add another row to our matrix (and another element
1
to our column vector). So the matrix for this example would be A = [1 1 2 c; 1 0 0 0; 0 1 0 0; 0 0 0 1]  if we take A*[f(n1); f(n2); f(n3); 1] we get [f(n); f(n1); f(n2); 1]@dreamlane Finding the matrix for exponentiation is usually not hit and trial. All recursions can be converted to matrices with sufficiently large dimensions. For this problem, 3x3 is enough.
So what does one need to study to derive these kind of formulas? Combinatorics maybe and linear algebra? Any suggestions are very welcome :)
Although i have solved this problem,but thanks for such a beautiful proof :)
@deathrash667 : I think a lil bit of Automata is required here.
Not necessarily all recurrences, definitely linear ones, and also ones like never_stop mentioned which I suppose could be called "affine". For matrix powers, we are formally using linear algebra but not really any deep results (essentially the def'n of matrix multiplication, and its associativity). Finding the matrix is just mechanical with the recurrence equation. As for going from a recurrence to a formula, I found knowing differential equation techniques helpful. You may also want to look at "generating functions" as they can make manipulating recurrences much easier.
I didn't understand why [f(2), f(1), f(0)] should be [4,2,1] No of valid strings of length 0 should be 0 right ?
@sudarshans the empty string is a valid string of length 0