Find the number of ways of distributing N objects into R groups such that each group…
- User
Find the number of ways of distributing N objects into R groups such that each group gets 1 or more objects. Input: The one and only line of input contains two numbers separated by a single space, which are N and R respectively.
Output: The corresponding answer modulo 10000007 in a single line and if no answer exist then print "-1".
Constraints: 1 ≤ N ≤ 100 1 ≤ R ≤ 100
SAMPLE INPUT 4 2
SAMPLE OUTPUT 3
Explanation
Let the 2 groups be A and B respectively. Case 1: A gets 1 object and B gets 3 Case 2: A gets 2 objects and B gets 2 Case 3: A gets 3 objects and B gets 1
Assistant
n, r = map(int, input().split()) mod = 10000007 if n < r: print(-1) else: max_n = n - 1 max_k = r - 1 # Create a DP table up to the required size dp = [[0] * (max_k + 1) for _ in range(max_n + 1)] for i in range(max_n + 1): dp[i][0] = 1 if i <= max_k: dp[i][i] = 1 for i in range(2, max_n + 1): for j in range(1, min(i, max_k) + 1): dp[i][j] = (dp[i-1][j-1] + dp[i-1][j]) % mod print(dp[max_n][max_k] % mod)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.