Page 1 of 2

10303 - How Many Trees?

Posted: Thu Jun 13, 2002 12:39 am
by Christian Schuster
After the input limit was set to 1000, I got some time problems, and - with BigNum and DP - I currently reach about 21 seconds. I'm using a sum of products to find the number. My question:

Is there an explicit formula to calculate those numbers?

Posted: Thu Jun 13, 2002 1:10 am
by Stefan Pochmann
If I remember it correctly, this is covered on page 181 in the second edition of the CLRS book. Don't know about the first edition. Look up "catalan numbers" in the index, it's a "problem", I believe. The problem brings you step by step to a nice formula that I think makes it easy to step from C(n) to C(n+1), so you can precalc all fast.

Correction: it's on page 271 (I knew 181 looked wrong, but it had the correct checksum. I guess I'll have to update my brain with something less error-prone)

Posted: Mon Jun 24, 2002 9:15 am
by Stefan Pochmann
Yep, just solved it. Starting with the formula of CLRS, you can get an easy recurrence relation of the form

c[0] = 1
c[n+1] = f( n, c[n] )

where f is really a simple function.

Let's ignore that working with large numbers takes more time than with simple ints, i.e. let's assume the arithmetic operations for large numbers can be done in constant time. Then I can precompute the first n Catalan numbers in time O(n).

Posted: Wed Jul 10, 2002 5:50 am
by yatsen
Can you give me some hint how I can find out the "simple" function f?

I use c(n)=c(0)*c(n-1)+c(1)*c(n-2)+......+c(n-1)*c(0)
And I got time limit exceed.

Posted: Wed Jul 10, 2002 11:01 am
by Stefan Pochmann
I think you should really start with the formula mentioned in CLRS. Here it is:

c(n) = 1/(n+1) * (2n choose n)

Then rewrite the "choose" part with something more useful and try it like this:

c(n+1) = ... = and-here-something-that-includes-c(n)-and-not-much-more.

Posted: Tue Jul 23, 2002 5:22 am
by yatsen
I know the answer is c(n)=(4n-2/n+1) c(n-1) by your hint.
Thank you very much!
But my question is how the fomula c(n) = 1/(n+1) * (2n choose n) comes from?
I am sorry I don't know anything about CLRS. Is there any reference I can find out for the formula in the internet?

10303 How Many Trees

Posted: Mon Oct 14, 2002 7:20 am
by rjhadley
CLRS is the book "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein. (ISBN 0262032937)

Posted: Thu Oct 17, 2002 7:24 pm
by LittleJohn
yatsen wrote: But my question is how the fomula c(n) = 1/(n+1) * (2n choose n) comes from?
I am sorry I don't know anything about CLRS. Is there any reference I can find out for the formula in the internet?
There's a good web site about math: http://mathworld.wolfram.com/
You may go and search for "Catalan Number."

Posted: Tue Oct 22, 2002 5:44 pm
by hs
Can anyone post the result for 1000?
I keep getting wrong answer!!!

Posted: Sun Oct 27, 2002 4:19 am
by Junayeed
i think this is the correct output for 1000:

2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120

Posted: Sun Oct 27, 2002 5:06 pm
by hs
Thanks, i finally solved it.

Posted: Mon Oct 28, 2002 4:29 am
by ithamar
Pleas can you kindly edit the message that shows the answer to 1000.

It makes the board wideeeeeeeeeeeeeeeer and it doesnt look right.

10303 Why WA???

Posted: Fri May 02, 2003 11:28 am
by medv
Help me please to solve 10303. I use formula
f(n) = 1/(n+1) * (2n choose n) = (n+2)*(n+3)*...*(2n) / (1*2*...*n).
All my tests seems right. Why WA?
I tried to change readln to read, integer to longint - but it didn't help me.
Help me if you can.

My code is here:

program How_Many_Trees_10303;
const
max = 1000;
Len = 1000;
var
i,j,z,t,n:longint;
m:array[1..max] of longint;
res:array[1..Len] of longint;
pres:longint;

function nsd(a:longint;b:longint):longint;
begin
if (a = 0) then nsd := b else
if (b = 0) then nsd := a else
if (a >= b) then nsd := nsd(a mod b, b) else
nsd := nsd(a, b mod a);
end;

procedure mult(nn:longint);
var
i,t:longint;
temp:array[1..Len] of longint;
begin
t := 0;
for i:=1 to pres do
begin
t := res[i]*nn + t;
temp[i] := t mod 10;
t := t div 10;
end;
while (t > 0) do
begin
Inc(pres);
temp[pres] := t mod 10;
t := t div 10;
end;
for i:=1 to pres do res[i] := temp[i];
end;

begin
read(n);
while True do
begin
for i:=2 to n do m[i] := n+i;
for i:=2 to n do
begin
z := i;
for j:=2 to n do
begin
t := nsd(z,m[j]);
z := z div t;
m[j] := m[j] div t;
if (z = 1) then break;
end;
end;
for i:=1 to Len do res[i] := 0; res[1] := 1; pres:=1;
for i:=2 to n do
if m[i] > 1 then
mult(m[i]);
for i:=pres downto 1 do write(res[i]);
read(n);
if (n > 0) then writeln else break;
end;
end.

Posted: Sun May 04, 2003 8:38 am
by ..
This is the output for 1000, are you correct on it?
2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120

My program seems correct - but WA

Posted: Sun May 04, 2003 12:17 pm
by medv
Thank you. Yes, my program gives correct solution when n=1000.
I tried to rewrite it in C, but also gets WA.

If you have accepted this problem, please - send me your solution.