All possible heaps

How many distinct (not necessarily binary) min-heaps can be constructed from the nodes with keys 1 , , 999999 1,\ldots,999999 ?

Give the answer modulo 1 0 9 + 7 10^{9}+7 .

Note :

Let be H 1 , H 2 H_1, H_2 two heaps: H 1 H 2 u v : u v H 1 u v H 2 H_1 \neq H_2 \iff \exists uv \; \; \textbf{:} \; \; uv \in H_1 \land uv \notin H_2


Inspiration .


The answer is 44455173.

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.

1 solution

Brian Riccardi
Feb 14, 2016

\textbf{Method 1. A little bit ugly.} \\ \textit{The idea: take \$1\$ as root and count how many different heaps I can make} \\ \textit{from a partition of \$n-1\$ nodes remaining.} \\ \text{Let \$f(n)\$ be the number of min-heaps with \$n\$ nodes and}\\ \text{\$g(n,k)\$ the number of min-heaps from all partitions of \$n\$ nodes in \$k\$ subsets.}\\ f(n)=\displaystyle\sum_{k=1}^{n-1}g(n-1,k) \\ \textit{The idea: once I picked the first element, I can pick the remainings \$h\$ in \${n-1} \choose {h}\$ ways,} \\ \textit{make \$f(h+1)\$ heaps from them and now I have the remaining \$n-h-1\$ nodes.} \\ g(n,k)= \begin{cases} 1 & if \; {n=0} \land {k=0} \\ 0 & if \; {n \lt k} \lor {k=0} \\ \displaystyle\sum_{h=0}^{n-1} {{n-1}\choose{h}}f(h+1)g(n-h-1,k-1) & otherwise \end{cases} \\ \\ \textbf{Method 2. More elegant.} \\ \textit{The idea: once I have all heaps from \$n-1\$ nodes I can add the node \$n\$ under the others.} \\ f(n)= \begin{cases} 1 & if \; n=1 \\ (n-1)f(n-1) & otherwise \end{cases} \\

\text{The answer is \${f(999999)} \mod {10^9+7} = \boxed{44455173}\$ } \\

EDIT. I noticed now that $f(n)=(n-1)!$, nice coincidence: can anyone explain why? :) \textbf{EDIT.} \\ \text{I noticed now that \$f(n)=(n-1)!\$, nice coincidence: can anyone explain why? :)} \\

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...