CT928

Derangements: Count Permutations with No Fixed Points

MediumAcceptance: 0.0%

In the Hat Check room of Prime City's Grand Ball, the Attendant must return hats to guests such that no one gets their own hat. The number of such arrangements is called a derangement ÔÇö a permutation where no element appears in its original position. "A derangement of n items is a permutation with no fixed points," the Attendant explains. "The formula is D(n) = (n-1) * (D(n-1) + D(n-2)), with D(0) = 1 and D(1) = 0. Alternatively, D(n) = n! * sum of (-1)^k / k! for k = 0 to n." Given n, compute the number of derangements of n items modulo 10^9 + 7. Constraints: 0 <= n <= 10^6 Input: 3 Output: 2 Input: 4 Output: 9

Constraints:

0 <= n <= 10^6

Tags:

derangement permutation combinatorics math
Loading...
Test Cases:No test cases
No test cases available.
Derangements: Count Permutations with No Fixed Points - MEDIUM Coding Problem | CodeTikki