Permutations and cycles

Given two natural numbers n and k, let f(n, k) denote the number of
permutations with n elements, and such that there are exactly k cycles,
all them of length at least 2. Implement a dynamic programming code to
compute f(n, k).

Input

Input consists of several cases, each with two natural numbers n and k.
You can assume 2 ≤ n ≤ 1000 and 1 ≤ k ≤ ⌊n/2⌋.

Output

For every case, print f(n, k). Because that number can become very
large, use @long long@’s and make the computations modulo 10⁹ + 7.

Hint

You can compute f(n, k) just adding two “recursive calls”.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T12:22:16.426Z

© Jutge.org, 2006–2026.
https://jutge.org
