forked from ivankra/acm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
x.c
48 lines (34 loc) · 690 Bytes
/
x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
/* Let G(n) be a tree on n vertices, with out-degree of each its
vertex limited to {0,2} */
/* Returns the number of different G(n) trees */
int g(int n)
{
int s, k;
if (n == 0 || n == 1)
return 1;
if (n < 0 || ((n % 2) == 0))
return 0;
for (s = 0, k = 0; k < n; k++)
s += g(k) * g(n - k - 1);
return s;
}
/* Returns the number of ordered forests of G(i) trees on m vertices */
int f(int m)
{
int s, k;
if (m < 0)
return 0;
if (m == 0)
return 1;
for (s = 0, k = 1; k <= m; k++)
s += g(k) * f(m - k);
return s;
}
int main()
{
int n;
while (scanf("%d", &n) == 1)
printf("f(%d) = %d, g(%d) = %d\n", n, f(n), n, g(n));
return 0;
}