You are on page 1of 7

THREE IS CROWD

Problem code: CROWD(Sept 12)


Two's company, three's a crowd!
It's been one year since Chef met his brother. Last year, his younger brother came to visit him
during this time of the year. This year, the Chef is planning to go visit his brother. Chef's
brother has planned to throw a "Welcome Party" for him. He wants to invite people from his
neighbourhood (i.e. from the street where he lives). There are N houses on the street in a
single line (not considering the brother's house). He wants the party to be fun and he will not
like to invite people who might spoil the mood of the party. If people are invited from three
consecutive houses on the street, they might create trouble. As they say, three's a crowd! He
doesn't want to ruin the Chef's Welcome Party and so he will not want to send invites to any
three consecutive houses. He wants you to tell him how many ways are there for him to go
wrong. Note that he can play safe by not inviting anyone to avoid a crowd.
Input:
First line of the input contains a single integer T, the number of test cases.

Each test case contains a line containing a single integer N described above.
Output:
For each test case output a single integer denoting the number of ways the brother can go
wrong with planning the party.

The answer can get quite large. So output the total number of ways modulo 10
9
+7.
Constraints:
1<=T<=10000
1<=N<=10
15


Example:
Input:
2
3
4

Output:
1
3

Explanation:
Case 1: The only way he can go wrong is by inviting all the houses.

Case 2: First way of getting wrong is by inviting houses (1,2,3). Second way to get wrong is
by inviting houses (2,3,4). Third way of going wrong is by inviting all 4 houses i.e. (1,2,3,4).
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 2
N
- f(N).
Since N can be very large, 2
N
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 2
N
.
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
0 10 110 0 10 0 0 10 110
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 M
N-2
. Once we
have M
N-2
, we can find the answer by multiplying M
N-2
to [4, 2, 1], and taking the first
value.
You can find M
N-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 :-)
MY FAIR COINS
Problem Code: CSUMD
All submissions for this problem are available.
There are coins of 2 different denominations in Crazyland, 1-cent coins and 2-cent coins. As
all the coins do, even these coins have two faces, i.e. heads and tails.
Your task is to find out the the number of ways to create a linear arrangement of these coins
so that their sum is N cents. The only condition on the linear arrangement is that
the first coin in the arrangement should always face heads. All other coins could either face
head or face tail.

Take N = 2 as an example. The possible arrangements are (1H, 1H), (2H), (1H, 1T), where
H is heads and T is tails.
Therefore there are 3 possible arrangements that sum up to 2-cents.
Note
While counting make sure that you count heads and tails as different.
Input
First line contains T, the number of cases.
T lines follow, each containing a single number N, the required sum.
Constraints
0 <= T <= 10000
1 <= N <= 1000000000
Output
For each case the output should be a single integer representing the number of such
arrangements possible. As this can be very large print it modulo 1000000007.
Sample Input
3
1
2
3
Sample Output
1
3
8
DIFFICULTY:
Simple-Easy
PREREQUISITES:
Linear Recurrences, Matrix Exponentiation
PROBLEM:
There are infinite coins of two types - of value 1 and of value 2. All coins have two sides -
heads and tails. You're to find out number of ways of arranging some coins such that their
sum is equal to N (input) and the first coin is heads up.
QUICK EXPLANATION:
Let f(N) denote number of ways of arranging some coins such that their sum is equal to N
and the first coin is heads up. Then it can be shown that f(N) = 2 (f(N-1) + f(N-2) ) with f(1)
= 1 and f(2) = 3. Using matrix exponentiation, f(N) can be computed in time O(log N).
DETAILED EXPLANATION:
Let f(n, H) denote the number of ways of arranging coins such that their sum is n, and the
first coin is heads up. Similarly let f(n, T) denote the number of ways of arranging coins such
that their sum is n and first coin is tails up.
Our original problem is to find out f(n, H).
Here are our three claims:
1. f(n, H) = f(n, T)
2. f(n, H) = f(n-1, H) + f(n-1, T) + f(n-2, H) + f(n-2, T)
3. f(n, H) = 2 * ( f(n-1, H) + f(n-2, H) )
Let's see why each of these claims hold:
1. f(n, H) = f(n, T)
This is true because if we invert every coin of an arrangement with sum of n and first
coin heads up, we get an arrangement with sum of n with first coin tails up. It is easy
to see that it is infact a bijection. Hence the claim.
2. f(n, H) = f(n-1, H) + f(n-1, T) + f(n-2, H) + f(n-2, T)
This is the chief step of the problem : Let's look at a particular arrangement with sum
of n and first coin heads up. First coin is either of value 1 or of value 2. In first case,
sum of remaining coins is n-1 and their first coin could be either heads up or heads
down. f(n-1, H) + f(n-1, T) account for such case. In other case, when first coin is of
value 2, sum of remaining coins is n-2 and they could either be heads up or tails up.
f(n-2, H) + f(n-2, T) account for these. So total : f(n, H) = f(n-1, H) + f(n-1, T) + f(n-
2, H) + f(n-2, T) holds.
3. f(n, H) = 2 * ( f(n-1, H) + f(n-2, H) )
This follows from claims 1 and 2 naturally :)
Now we can drop H from the notation to get f(n) = 2 f(n-1) + 2 f(n-2) with base case of this
recurrence as f(1) = 1 and f(2) = 3. We can use matrix exponentiaion to solve for this
recurrence. For those of you who don't know this very useful technique, I'm providing a short
description here itself.
Let's write it in form of matrices:
[ f(n) f(n-1) ] = [ f(n-1) f(n-2) ] * | 2 1 |
| 2 0 |
You can verify that this is nothing but the recurrence we just wrote. Special property of
writing it in this form is we [ f(n-1) f(n-2) ] is exactly of the same form as [ f(n) f(n-1) ] and
so we can in turn expand it to get:
[ f(n) f(n-1) ] = [ f(n-2) f(n-3) ] * | 2 1 | * | 2 1 |
| 2 0 | | 2 0 |
We can continue expanding term on right handside till we get :
[ f(n) f(n-1) ] = [ f(2) f(1) ] * | 2 1 |^(n-2)
| 2 0 |
We can do a matrix exponentiation in time O(log N) using repeated squaring. So we can find
out f(N) as well in O(log N) time as well. As we've to report final answer modulo (10^9 + 7),
we would do all operations modulo (10^9 + 7)

You might also like