The elder binary trees

A binary tree is called ancestral if each node has either 0 or 2 child nodes. Let N N be the number of different ancestral trees there are with 2015 nodes. Find the last three digits of N N .


The answer is 480.

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.

3 solutions

Ivan Koswara
Jul 26, 2015

Such ancestral binary tree is normally called a full binary tree. It is well-known that the number of full binary trees with 2 n + 1 2n+1 vertices is C n C_n , where C n C_n is the n n -th Catalan number given as 1 n + 1 ( 2 n n ) \frac{1}{n+1} \binom{2n}{n} . (One simple proof is to consider the sizes of the subtrees of the root, and compare it with the identity C n + 1 = i = 0 n C i C n i C_{n+1} = \sum_{i=0}^n C_i C_{n-i} .) Simply plugging this in, we want to find the last three digits of C 1007 = 1 1008 ( 2014 1007 ) C_{1007} = \frac{1}{1008} \binom{2014}{1007} ; with some language that supports arbitrary precision integer (like Java or Python), we can compute that this 602-digit number ends with 480 \boxed{480} .

Moderator note:

Yes, that's the formula for a full binary tree.

Can we find the last 3 digits without a computer?

Hint: Consider ( 2014 1007 ) ( m o d 1000 × 1008 ) { 2014 \choose 1007} \pmod{ 1000 \times 1008 } .

All ancestral trees have an odd number of nodes. A tree with 2 n + 1 2n+1 nodes consists of a top node, and continues with trees of sizes 2 k + 1 2k+1 and 2 k + 1 2k'+1 . We have k + k = n 1 k + k' = n-1 .

The numbers get large quickly! Therefore I truncate all values modulo 1000. After all, we only need the last three digits. This code gives the correct answer of 480 \boxed{480} :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
        int trees[] = new int[1008];            // trees[n] = ways to make 2n+1 tree

        trees[0] = 1;

        for (int N = 1; N <= 1007; N++) {
            trees[N] = 0;

            for (int k = 0; k < N; k++) {
                trees[N] += trees[k] * trees[N-k-1];
                trees[N] %= 1000;
            }
        }

        out.println(trees[1007]);

Ww Margera
Aug 20, 2015

Simple python:

l=[0,1]
for i in range(2,2016):
  s=0
  for j in range(1,i):
    s+=l[j]*l[i-1-j]
  l.append(s%1000)
print(l[2015])

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...