10303 - How Many Trees?
Moderator: Board moderators
-
- Learning poster
- Posts: 63
- Joined: Thu Apr 04, 2002 2:00 am
10303 - How Many Trees?
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?
Is there an explicit formula to calculate those numbers?
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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)
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)
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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).
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).
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
10303 How Many Trees
CLRS is the book "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein. (ISBN 0262032937)
-
- Learning poster
- Posts: 83
- Joined: Wed Feb 27, 2002 2:00 am
- Location: Taiwan
There's a good web site about math: http://mathworld.wolfram.com/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?
You may go and search for "Catalan Number."
i think this is the correct output for 1000:
2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120
2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120
10303 Why WA???
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.
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.
This is the output for 1000, are you correct on it?
2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120
2046105521468021692642519982997827217179245642339057975844538099572176010191891863964968026156453752449015750569428595097318163634370154637380666882886375203359653243390929717431080443509007504772912973142253209352126946839844796747697638537600100637918819326569730982083021538057087711176285777909275869648636874856805956580057673173655666887003493944650164153396910927037406301799052584663611016897272893305532116292143271037140718751625839812072682464343153792956281748582435751481498598087586998603921577523657477775758899987954012641033870640665444651660246024318184109046864244732001962029120
My signature:
- Please make discussion about the algorithm BRFORE posting source code.
We can learn much more in discussion than reading source code. - I HATE testing account.
- Don't send me source code for debug.
My program seems correct - but WA
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.
I tried to rewrite it in C, but also gets WA.
If you have accepted this problem, please - send me your solution.