Alice and Bob challenged each other to a duel with at most 30 rounds. A person will win when they have won precisely 3 more rounds than the other. In how many different sequences can Alice win?
Denote the round where Alice or Bob wins as
A
and
B
respectively. Some winning sequences for Alice are as follows:
1 2 3 |
|
Note that the sequence
BBBAAAAAA
is not a winning sequence for Alice because Bob already won on the third round and the game would have ended.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
@Mark Hennings What do you mean by "a simple "Pascal's Triangle" argument" ? Is there an easier method to find the matrix than expanding ?
Log in to reply
Find the number of ways that Alice can have a lead of j after n + 1 rounds by considering what her lead can be after n rounds, and thinking who won the last round. For example x ( n + 1 ) 2 = x ( n ) 1 , since the only way that Alice can have a lead of 2 after n + 1 rounds is if she has a lead of 1 after n rounds, and then wins the last round (she cannot have a lead of 3 after n rounds without the game ending then).
You could try for the eigenvalues/vectors of the matrix, I guess.
I don't know what dynamic programming is, any way here is classic "programming way" of doing it :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Thanks for the problem.
The answer is 239 thousand ,unless you don't want your comp to work for the next few hour ,pls carry on
This is a dynamic programming problem. Let's see why:
In essence, we need to keep track of just the previous states.
Let us keep two variables:
alice[i][n]
denoting the number of sequences of length
n
in which Alice is ahead by
i
points.
bob[i][n]
denoting the number of sequences of length
n
in which Bob is ahead by
i
points.
Now, using the above reasoning, we could say that:
1 2 3 4 5 6 |
|
The base case, certainly is:
1 2 |
|
for all i.
To get the value of the answer, we would need to compute another additional value for Alice, the winning sequences:
1 |
|
What we need to compute is the sum of all values of
alice[3][n]
from
n
= 1 to 30
See Christopher's solution for the code.
My solution in python 3
1 2 3 4 5 6 7 8 9 |
|
Output
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
dp[0][i]
is the number of valid sequence (no one wins yet) such that Alice is 2 more than Bob.
dp[1][i]
is the number of valid sequence (no one wins yet) such that Alice is 1 more than Bob.
dp[2][i]
is the number of valid sequence (no one wins yet) such that Alice is the same as Bob.
dp[3][i]
is the number of valid sequence (no one wins yet) such that Bob is 1 more than Alice.
dp[4][i]
is the number of valid sequence (no one wins yet) such that Bob is 2 more than Alice.
The base case is simple. For
i
=
0
,
dp[2][0] = 1
and others are
0
. Then, the transition is pretty interesting too :
dp[k][i] = dp[k-1][i-1] + dp[k+1][i]
. This is due to either Alice or Bob wins can generate
dp[k][i]
.
Let f r ( n ) be the number of possible sequences of length n at the end of which Alice is r rounds ahead. (E.g. f 2 ( 4 ) = 3 .)
The number of ways Alice can win after at most 3 0 rounds is f 2 ( 2 9 ) + f 2 ( 2 8 ) + f 2 ( 2 7 ) + . . . .
If Alice is 2 rounds ahead, 1 round earlier she must have been 1 round ahead. f 2 ( n ) = f 1 ( n − 1 )
f 1 ( n ) = f 2 ( n − 1 ) + f 0 ( n − 1 )
f 0 ( n ) = 2 f 1 ( n − 1 )
Therefore f 1 ( n ) = 3 f 1 ( n − 2 ) ) f 1 ( 2 k ) = 0 , f 1 ( 1 ) = 1 , so f 1 ( 2 k − 1 ) = 3 k
The number we seek is f 2 ( 2 9 ) + f 2 ( 2 8 ) + f 2 ( 2 7 ) + . . . = f 1 ( 2 8 ) + f 1 ( 2 7 ) + f 1 ( 2 6 ) + . . . = 3 1 3 + 3 1 2 + . . . + 1 = 2 3 1 4 − 1
Problem Loading...
Note Loading...
Set Loading...
This is more a computation problem than one of dynamic programming. If x ( n ) j is the number of ways that Alice can have a lead of j after n rounds, with the game not having already ended, and if x ( n ) is the column vector x ( n ) = ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ x ( n ) 3 x ( n ) 2 x ( n ) 1 x ( n ) 0 x ( n ) − 1 x ( n ) − 2 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ for any n ∈ N , then (by a simple "Pascal's Triangle" argument) x ( n + 1 ) = ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ x ( n ) for any n ∈ N , and hence x ( n ) = ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ n ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 0 0 0 1 0 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ and hence the number of ways is n = 1 ∑ 3 0 x ( n ) 3 = = = n = 1 ∑ 3 0 ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 1 0 0 0 0 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ T ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ n ⎝ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎛ 0 0 0 1 0 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎞ 0 + 0 + 1 + 0 + 3 + 0 + 9 + 0 + 2 7 + 0 + 8 1 + 0 + 2 4 3 + 0 + 7 2 9 + 0 + 2 1 8 7 + 0 + 6 5 6 1 + 0 + 1 9 6 8 3 + 0 + 5 9 0 4 9 + 0 + 1 7 7 1 4 7 + 0 + 5 3 1 4 4 1 + 0 + 1 5 9 4 3 2 3 + 0 2 3 9 1 4 8 4