]> mj.ucw.cz Git - saga.git/commitdiff
Added a program for computing the n0's.
authorMartin Mares <mj@ucw.cz>
Wed, 27 Feb 2008 13:38:38 +0000 (14:38 +0100)
committerMartin Mares <mj@ucw.cz>
Wed, 27 Feb 2008 13:38:38 +0000 (14:38 +0100)
programs/n0.c [new file with mode: 0644]

diff --git a/programs/n0.c b/programs/n0.c
new file mode 100644 (file)
index 0000000..36da613
--- /dev/null
@@ -0,0 +1,77 @@
+#include <stdio.h>
+
+#define MAX 13
+
+// Faktorial
+int f(int n)
+{
+  static int ff[MAX] = { 1 };
+  if (!ff[n])
+    ff[n] = n*f(n-1);
+  return ff[n];
+}
+
+// Satnarka
+int s(int d)
+{
+  int r = 0;
+  int sg = 1;
+  for (int k=0; k<=d; k++)
+    {
+      r += sg*f(d)/f(k);
+      sg = -sg;
+    }
+  return r;
+}
+
+// Castecne restrikce
+int n0(int z, int d)
+{
+  static int nn[MAX][MAX];
+  if (!nn[z][d])
+    {
+      if (!z)
+       nn[z][d] = f(d);
+      else if (z == d)
+       nn[z][d] = s(d);
+      else
+       nn[z][d] = z*n0(z-1,d-1) + (d-z)*n0(z,d-1);
+    }
+  return nn[z][d];
+}
+
+int main(void)
+{
+  for (int i=0; i<MAX; i++)
+    printf("\t%d", i);
+  putchar('\n');
+  for (int i=0; i<MAX; i++)
+    {
+      printf("%d\t", i);
+      for (int j=0; j<MAX; j++)
+       {
+         if (j >= i)
+           printf("%d", n0(i, j));
+         putchar('\t');
+       }
+      putchar('\n');
+    }
+  putchar('\n');
+
+  for (int i=0; i<MAX; i++)
+    {
+      printf("%d\t", i);
+      for (int j=0; j<MAX; j++)
+       {
+         if (j >= i && i > 0)
+           {
+             double d = (double)(n0(i-1,j)-n0(i,j))/n0(i,j);
+             printf("%5.2f", 1/d);
+           }
+         putchar('\t');
+       }
+      putchar('\n');
+    }
+
+  return 0;
+}