Problem Description
Given n unique candies and k bags, count the number of ways to distribute all the candies into the bags so that every bag contains at least one candy. Two distributions are considered different if there exists at least one bag whose set of candies in one distribution does not exactly match any bag's set in the other distribution. Return the answer modulo 10^9 + 7.
Key Insights
- The problem is equivalent to partitioning n distinct items into k non-empty, unlabeled subsets.
- This counting is given by the Stirling numbers of the second kind.
- The recurrence relation is: dp[n][k] = dp[n-1][k-1] + k * dp[n-1][k].
- Base condition: dp[0][0] = 1, with all other invalid partitions being 0.
- Use dynamic programming with modulo arithmetic to efficiently compute the result.
Space and Time Complexity
Time Complexity: O(n * k)
Space Complexity: O(n * k)
Solution
We employ a dynamic programming approach using a 2D table dp where dp[i][j] represents the number of ways to partition i candies into j bags. For each candy, we either start a new bag (using dp[i-1][j-1]) or place it in one of the j existing bags (contributing j * dp[i-1][j]). We take all operations modulo 10^9 + 7 to manage large numbers. This method ensures that every bag has at least one candy and computes the answer efficiently.