A sequence of + and - signs, such as this one:
+-++-+-++---
can be transformed into a sequence of integers according to the following rules:
For example, the sequence +-++-+-++--- gives rise to the following sequence of integers: 0, 1, 0, 1, 2, 1, 2, 1, 2, 3, 2, 1, 0.
Call a sequence of + and - signs good if the resulting sequence of integers contains no negative numbers and ends with 0. Our sequence +-++-+-++--- is a good sequence of length 12. The sequences +++-- and +--+ are not good.
Let N be the number of all good sequences of + and - signs of length 40. Find the first three digits of N .
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.
Really cool,how you related this with the parentheses problem,I didn't think of it that way..Now I know how IDES work.
Yes, this is similar to how I approached it - I tested a couple values of lengths (length 2, 4, 6, etc.) and found that the resulting sequence was the Catalan Numbers, so I tried to prove why this is the case, and since (a closed form of) the series for catalan numbers is
C n = n ( n + 1 2 n )
We can plug in n = 2 0 and obtain 6 5 6 4 1 2 0 4 2 0 ⟹ 6 5 6
We can use dynamic programming to solve the problem.
Let d p ( p , m ) be the number of sequences such that the resulting sequence of integers as described in the question contains no negative numbers. Since the right-most sign must be a '+' or '-', we have d p ( p , m ) = d p ( p − 1 , m ) + d p ( p , m − 1 ) in general.
To find the base cases, we observe that the last integer in the sequence of integers is equal to the number of '+' signs minus the number of '-' signs and since this integer cannot be negative, we must have p ≥ m so when p < m , d p ( p , m ) = 0 . Furthermore, when m = 0 , there is only 1 possible sequence which is a sequence of '+' signs so d p ( p , 0 ) = 1 where p is non-negative.
For a sequence to be good, the last integer in the sequence formed must be 0 so the number of '+' signs equals the number of '-' signs. Hence the number of good sequences of length 4 0 is d p ( 2 0 , 2 0 ) = 6 5 6 4 1 2 0 4 2 0
We could solve this problem by using Dynamic Programming Approach
Let f ( l e n , s c o r e ) be the number of different sequences with length l e n with s c o r e as the last element.
thus by using the recurrence function f ( l e n , s c o r e ) = ⎩ ⎨ ⎧ 0 1 f ( l e n − 1 , s c o r e − 1 ) + f ( l e n − 1 , s c o r e + 1 ) if $score$ < 0 if $len$ = 1 and $score$ = 0
from the function above, we could get the answer from f ( 4 0 , 0 ) , thus we get 6 5 6 4 1 2 0 4 2 0 . hence the first 3 digit is 6 5 6
we could implement the reccurence function by the Top-Down Memoization Approach, here my C/C++ code
for a good sequence of length 2 n number of '+'s = '-'s= n
number of ways to distribute them C(2 n , n )
from combinatorics we know that for the '+'s to outnumber '-'s at every point only one possibility remains out of n , and because any good sequence starts with '+' and ends with '-' (to avoid negatives that would appear otherwise) it turns out that N(n) = C(2 n , n )/( n +1) (fraction formating \frac{a}{b} not working)
substituting n =20 we get N(20)= 6564120420 = 656
Call a sequence with no negative numbers, a meh sequence.A meh sequence ending in 0 is a good one. Let's count the sequences recursively.
We introduce a function f ( n , k ) , which counts the number of meh sequences ending in k of length n , where the initial 0 is not counted in our length, so it coincides with the length of the sign sequence. The problem asks for f ( 4 0 , 0 ) . Also, let a i be a number in our number sequence, with a 0 = 0 .
Notice that a n = k , and because we can only add or subtract 1 to adjacent members of the sequence, we can only have a n − 1 = k − 1 , k + 1 .
Therefore for every meh sequence of length n − 1 ending with k − 1 we can add a + sign in the sign sequence to form a new meh sequence of length n ending with k . Similarly, we can add a − sign with the sequences of length n − 1 ending with k + 1 . We counted all the meh sequences of length n because these are the only two cases and they don't overlap.
Obviously, since we can't admit negatives, for the special case k = 0 , we can only have a n − 1 = 1 .
Also notice that we must start with a + sign otherwise a 1 would be negative.
Therefore we have the following recursion f ( 1 , 1 ) = 1 f ( 1 , k ) = 0 if k = 1 f ( n , 0 ) = f ( n − 1 , 1 ) f ( n , k ) = f ( n − 1 , k − 1 ) + f ( n − 1 , k + 1 ) if k ≥ 1
We can then just use dynamic programming to compute the answer.
My code in Python 3, I use lru_cache for lazy dynamic programming, but you can just use a regular list or dictionary if you want.
from functools import lru_cache
@lru_cache(maxsize=None)
def f(n,k):
if (n,k) == (1,1):
return 1
if n == 1:
return 0
if k==0:
return f(n-1,1)
return f(n-1,k-1)+f(n-1,k+1)
print(f(40,0))
Once Again,Recursion with Memoization.
F(N,M) is the number of ways of making a good sequence that sums to N using M moves for N>=0.We use N=0 for our case. Code uses the fact that F(N,M)=F(N-1,M-1)+F(N+1,M-1)
Store = {}
def F( N , M ):
if (N%2)!= (M%2):return 0 #N and M must either both be odd or both be even;
if ( N , M ) in Store:#If it has already computed,avoid recomputation and obtain from dictionary
return Store[ ( N , M ) ]
if N < 0:return 0 #Define a base case
if M == 0:
if N == 0:return 1 # Also a base case F(0,0) has to be 1
return 0
a = F( N - 1 , M - 1) # recursive part
b = F( N + 1 , M - 1) #recursive part
Store[ ( N , M ) ] = a + b
return a + b
print str( F( 0 , 40 ) )[0:3]
Via a combinatorial reduction, we can look at this problem as the problem of matching + and − , which is equivalent to the problem of counting binary trees: + ( + − ) ( + − ) − ≅ ∘ ↙ ∘ ↘ ∘ This language is succinctly described by the regular pattern T : = ∅ ∣ + T 1 T 2 − transferring into the reals, this translates into the function T ( z ) = 1 + z T ( z ) 2 ⟹ T ( z ) = 2 z 1 − 1 − 4 z (we need the minus because otherwise T isn't analytic around z = 0 , so we can't use Taylor's theorem) We can extract out the coefficients via the generalized binomial theorem 2 z 1 − 1 − 4 z = 2 1 z 1 + ∑ k ( k 1 / 2 ) ( − 4 z ) k = 2 1 k ∑ ( k + 1 1 / 2 ) ( − 4 ) k + 1 z k = k ∑ k + 1 1 ( k 2 k ) z k Therefore, for a class of strings of size 2 k , we would expect there to be k + 1 1 ( k 2 k ) good sequences. Therefore, the answer is 2 1 1 ( 2 0 4 0 ) = 6 5 6 4 1 2 0 4 2 0
I have no Idea for programm, but I Know it's "Catalan Number Problem for n = 2 0 "
So, the answer are:
2 1 C ( 4 0 , 2 0 ) = 6 5 6 4 1 2 0 4 2 0
The first three digit 6 5 6
long int numstr( long int a, long int b )
{
if (a==1) return b;
if (a==b) return numstr(a-1,b) ;
return ( numstr(a-1,b) + numstr(a, b-1) );
}
int main()
{
long int n;
clrscr();
n=numstr(20,20);
printf("number of string = %ld",n);
getchar();
return 0;
}
We use dynamic programming. We define dp[i][j] as the number of non-negative sequences of length i that sum to j. One can then find the transition: dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1]. As our base case, dp[0][0] = 1, and dp[0][x] = 0 if x > 0. Additionally, dp[i][k] = 0 if k < 0. My code can be found here: http://ideone.com/trc1tN
The link is not working. By the way how did you learnt dynamic programming. Can you suggest any tutorial or something?
Log in to reply
This is the best DP tutorial I know. You could also look at MIT's 6.00 course..If I recall correctly one or two fo the lectures are dedicated to DP and the knapsack problem
I actually see that the number of good sequences of length 2 n is the ( n + 1 ) th Catalan number. Not a computer science solution. Answer is 6 5 6 4 1 2 0 4 2 0 .
Not much computer science actually used. The answer is the 4 0 / 2 = 2 0 t h Catalan number C 2 0 . The Catalan numbers are given by C n = n + 1 1 ( n 2 n ) = ( n + 1 ) ! n ! 2 n ! . This can be calculated with your language of choice in many different ways, but the answer will be 6 5 6 4 1 2 0 4 2 0 , so the first three digits are 6 5 6 .
If you don't know of the Catalan numbers, look them up! They satisfy a whole slew of recurrence relations and combinatorics problems.
Problem Loading...
Note Loading...
Set Loading...
This problem maps directly to the Parentheses problem:
How many different ways is it possible to arrange n pairs of parentheses such that:
Each "+" maps to "(", and each "-" maps to ")".
The answer to the parentheses problem is C n = n + 1 1 ( n 2 n ) . See Catalan numbers .
We have 20 pairs of pluses and minuses, so C 2 0 = 2 1 1 ( 2 0 4 0 ) = 6 5 6 4 1 2 0 4 2 0 .
The first three digits are hence 6 5 6 .