#P5259. Hand‐Holding Circles

    ID: 18495 Type: Default 1000ms 256MiB

Hand‐Holding Circles

Hand‐Holding Circles

Consider a group of (N) students numbered from 1 to (N). Each student holds the hands of two different other students. The connections are made under the following rules:

  1. Each student uses their right hand to hold exactly one other student's left hand and, simultaneously, their left hand is held by exactly one other student's right hand.
  2. A student cannot hold their own hand, i.e. if student (i) holds student (j)'s left hand with their right hand then (i \neq j).
  3. Moreover, the two students whose hands a given student holds must be different. In other words, if student (i) holds student (j)'s left hand (using their right hand) and someone holds student (i)'s left hand, that student must be different from (j).

Under these conditions, the hand‐holding connections define a permutation (f) on ({1,2,\dots, N}) by letting (f(i)) be the student whose left hand is held by student (i)'s right hand. Notice that the left hand of student (i) is held by (f^{-1}(i)). The requirement that the two persons connected to each student are distinct implies that for every (i) we must have (f(i) \neq f^{-1}(i)\n(This excludes cycles of length 2). Also, since no student holds their own hand, (f(i)\neq i) for every (i).

Thus, the permutation (f) is a derangement with no 2-cycles (i.e. every cycle has a length of at least 3). Such a permutation decomposes into disjoint cycles. Given a positive integer (k), we are interested in the number of essentially different hand‐holding arrangements (schemes) in which the permutation (f) decomposes into exactly (k) disjoint cycles. Two schemes are considered essentially different if there exists at least one student and one of his hands such that the student holding that hand is different in the two schemes.

Your task is to compute the number of valid hand‐holding arrangements for given (N) and (k). It is guaranteed that (N) is small (for example, (3 \le N \le 20)) so that the answer can be computed exactly using the recurrence [ \mathit{dp}[n][k] = \sum_{L=3}^{n} \binom{n-1}{L-1} (L-1)! ; \mathit{dp}[n-L][k-1], \quad \mathit{dp}[0][0]=1, ] where (L) represents the length of the cycle chosen. The final answer is (\mathit{dp}[N][k]).

inputFormat

The input consists of a single line containing two integers (N) and (k) separated by a space, where (N) is the number of students and (k) is the desired number of disjoint cycles in the hand‐holding arrangement.

outputFormat

Output a single integer, the number of valid (essentially different) hand‐holding arrangements that result in exactly (k) cycles.

sample

3 1
2