Number of Ways to Rearrange Sticks With K Sticks Visible LeetCode solution in Python
1M = 10**9 + 723from functools import cache456class Solution:7def rearrangeSticks(self, n: int, k: int) -> int:8return dp(n, k)91011# consider the shortest12@cache13def dp(n, k):14if n == k:15return 116if n <= 0 or k <= 0:17return 018return (dp(n - 1, k - 1) + (n - 1) * dp(n - 1, k)) % M192021# `dp(n, k)` indicates the number of arrangements of `n` distinct sticks with exactly `k` visible sticks.22# Those `n` **distinct** sticks does NOT have to be numbered from `1` to `n` consecutively.