#K11306. Counting Labeling Ways for k-Trees
Counting Labeling Ways for k-Trees
Counting Labeling Ways for k-Trees
Given an n-node k-tree, you are required to compute the number of distinct ways to label this tree. In a k-tree where every node may have at most k children, the number of ways to label the tree is defined by the factorial of n-1 when n > 1 and is 1 when n ≤ 1.
More formally, let \(T(n,k)\) denote the number of labeling ways for an n-node k-tree. Then for \(n \geq 2\):
[ T(n,k) = (n-1)! \quad \text{and} \quad T(0,k) = T(1,k) = 1. ]
Note: Although the parameter k is provided as input, the formula does not depend on it.
inputFormat
The input consists of a single line containing two space-separated integers n and k, where n (\(0 \leq n \leq 10^5\)) is the number of nodes in the tree, and k is the maximum number of children each node can have.
outputFormat
Output a single integer representing the number of distinct ways to label the given k-tree. The answer is computed as follows:
[ \text{Answer} = \begin{cases} 1, & \text{if } n \leq 1 \ (n-1)!, & \text{if } n \geq 2 \end{cases} ]
sample
1 2
1