There are n children and m apples that will be distributed to them. Your task is to count the number of ways this can be done.
Examples:
Input: n = 3, m = 2
Output: 6
Explanation: There are 6 ways to distribute 2 apples among 3 children: [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0] and [2, 0, 0].Input: n = 4, m = 2
Output: 10
Explanation: There are 10 ways to distribute 2 apples among 4 children: [0, 0, 0, 2], [0, 0, 2, 0], [0, 2, 0, 0], [2, 0, 0, 0], [0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0], [1, 0, 1, 0] and [1, 1, 0, 0]
Approach: To solve the problem, follow the below idea:
To solve this problem, we can use the concept of the Combinatorics and Precomputation. The number of ways to the distribute m apples among n children is equivalent to finding the number of the combinations of the selecting n−1 separators from the total of the m+n−1 positions. This is because the apples and children can be represented as a sequence of the apples followed by the n−1 separators where each separator represents the boundary between the two consecutive children.
Step-by-step algorithm:
- Calculate the total number of the positions (apples + separators).
- Initialize the result as 1.
- Calculate the number of the combinations using the formula.
- Iterate from 1 to n-1 and update the result accordingly.
- Return the final result as the number of ways to the distribute the apples among the children.
Below is the implementation of the algorithm:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxN = 2e6;
const ll MOD = 1e9 + 7;
ll fact[maxN], inv[maxN];
// Function to calculate modular inverse of a number
ll getInverse(ll x)
{
ll ans = 1;
ll base = x;
ll expo = MOD - 2;
while (expo) {
if (expo & 1)
ans = (ans * base) % MOD;
base = (base * base) % MOD;
expo >>= 1;
}
return ans;
}
// Function to calculate nCr
ll C(int n, int r)
{
ll res = 1;
r = min(n - r, r);
for (int i = 0; i < r; i++) {
res = (res * 1LL * (n - i)) % MOD;
res = (res * 1LL * getInverse(i + 1)) % MOD;
}
return res;
}
// drive code
int main()
{
// Sample Input
int n = 4, m = 2;
// Print the number of ways to the distribute the apples
// among the children
cout << C(n + m - 1, m);
}
import java.math.BigInteger;
public class Main {
// Define the MOD constant
static final BigInteger MOD
= new BigInteger("1000000007");
// Function to calculate modular inverse of a number
static BigInteger getInverse(BigInteger x)
{
return x.modPow(MOD.subtract(BigInteger.TWO), MOD);
}
// Function to calculate nCr
static BigInteger C(int n, int r)
{
BigInteger res = BigInteger.ONE;
r = Math.min(n - r, r);
for (int i = 0; i < r; i++) {
res = res.multiply(BigInteger.valueOf(n - i))
.mod(MOD);
res = res
.multiply(getInverse(
BigInteger.valueOf(i + 1)))
.mod(MOD);
}
return res;
}
// Driver code
public static void main(String[] args)
{
// Sample Input
int n = 4, m = 2;
// Print the number of ways to distribute the apples
// among the children
System.out.println(C(n + m - 1, m));
}
}
MOD = int(1e9) + 7
# Function to calculate modular inverse of the number
def get_inverse(x):
ans = 1
base = x
expo = MOD - 2
while expo:
if expo & 1:
ans = (ans * base) % MOD
base = (base * base) % MOD
expo >>= 1
return ans
# Function to calculate nCr
def C(n, r):
res = 1
r = min(n - r, r)
for i in range(r):
res = (res * (n - i)) % MOD
res = (res * get_inverse(i + 1)) % MOD
return res
# Sample Input
n = 4
m = 2
# Print the number of ways to the distribute the apples among the children
print(C(n + m - 1, m))
const MOD = BigInt(1e9 + 7);
// Function to calculate modular inverse of a number
function getInverse(x) {
return x ** (MOD - BigInt(2)) % MOD;
}
// Function to calculate nCr
function C(n, r) {
let res = BigInt(1);
r = Math.min(n - r, r);
for (let i = 0; i < r; i++) {
res = res * BigInt(n - i) % MOD;
res = res * getInverse(BigInt(i + 1)) % MOD;
}
return res;
}
// Main driver code
function main() {
// Sample Input
const n = 4, m = 2;
// Print the number of ways to distribute the apples among the children
console.log(C(n + m - 1, m).toString());
}
// Calling the main function
main();
Output
10
Time Complexity: N*logN
Auxiliary Space: O(1)