从 P 个男孩和 Q 个女孩中选择 N 个包含至少 4 个男孩和 1 个女孩的人的方法计数
给定整数N 、 P和Q ,任务是从P 个男孩和Q 个女孩中找出由N 个人组成的一组至少有 4 个男孩和1 个女孩的方法数。
例子:
Input: P = 5, Q = 2, N = 5
Output: 10
Explanation: Suppose given pool is {m1, m2, m3, m4, m5} and {w1, w2}. Then possible combinations are:
m1 m2 m3 m4 w1
m2 m3 m4 m5 w1
m1 m3 m4 m5 w1
m1 m2 m4 m5 w1
m1 m2 m3 m5 w1
m1 m2 m3 m4 w2
m2 m3 m4 m5 w2
m1 m3 m4 m5 w2
m1 m2 m4 m5 w2
m1 m2 m3 m5 w2
Hence the count is 10.
Input: P = 5, Q = 2, N = 6
Output: 7
方法:这个问题是基于组合数学的,我们需要从可用的1个男孩中选择至少4 个男孩,并且从可用的Q个女孩中选择至少Y个女孩,因此选择的总人数是 N。
考虑这个例子:
P = 5, Q = 2, N = 6
In this, the possible selections are:
(4 boys out of 5) * (2 girls out of 2) + (5 boys out of 5) * (1 girl out of 2)
= 5C4 * 2C2 + 5C5 * 2C1
因此,对于P 、 Q和N的一些一般值,该方法可以可视化为:
在哪里
按照下面提到的步骤来实现它:
- 从i = 4开始迭代循环直到i = P 。
- 在每次迭代中,如果我们选择i 个男孩和(Ni) 个女孩,使用组合计算可能方式的数量
- 将每次迭代的可能值添加为方式的总数。
- 最后返回总计算方式。
下面是该方法的实现:
C++
#include
using namespace std;
// Function to calculate factorial
long long int fact(int f)
{
f++;
long long int ans = 1;
// Loop to calculate factorial of f
while (--f > 0)
ans = ans * f;
return ans;
}
// Function to calculate combination nCr
long long int ncr(int n, int r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
// Function to calculate the number of ways
long long int countWays(int n, int p, int q)
{
long long int sum = 0;
// Loop to calculate the number of ways
for (long long int i = 4; i <= p; i++) {
if (n - i >= 1 && n - i <= q)
sum += (ncr(p, i) * ncr(q, n - i));
}
return sum;
}
// Driver code
int main()
{
int P = 5, Q = 2, N = 5;
cout << countWays(N, P, Q) << endl;
return 0;
}
Java
import java.util.*;
class GFG{
// Function to calculate factorial
static int fact(int f)
{
f++;
int ans = 1;
// Loop to calculate factorial of f
while (--f > 0)
ans = ans * f;
return ans;
}
// Function to calculate combination nCr
static int ncr(int n, int r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
// Function to calculate the number of ways
static int countWays(int n, int p, int q)
{
int sum = 0;
// Loop to calculate the number of ways
for (int i = 4; i <= p; i++) {
if (n - i >= 1 && n - i <= q)
sum += (ncr(p, i) * ncr(q, n - i));
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int P = 5, Q = 2, N = 5;
System.out.print(countWays(N, P, Q) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Function to calculate factorial
def fact(f):
ans = 1
# Loop to calculate factorial of f
while (f):
ans = ans * f
f -= 1
return ans
# Function to calculate combination nCr
def ncr(n, r):
return (fact(n) / (fact(r) * fact(n - r)))
# Function to calculate the number of ways
def countWays(n, p, q):
sum = 0
# Loop to calculate the number of ways
for i in range(4, p + 1):
if (n - i >= 1 and n - i <= q):
sum += (ncr(p, i) * ncr(q, n - i))
return (int)(sum)
# Driver code
P = 5
Q = 2
N = 5
print(countWays(N, P, Q))
# This code is contributed by gfgking.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to calculate factorial
static int fact(int f)
{
f++;
int ans = 1;
// Loop to calculate factorial of f
while (--f > 0)
ans = ans * f;
return ans;
}
// Function to calculate combination nCr
static int ncr(int n, int r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
// Function to calculate the number of ways
static int countWays(int n, int p, int q)
{
int sum = 0;
// Loop to calculate the number of ways
for (int i = 4; i <= p; i++) {
if (n - i >= 1 && n - i <= q)
sum += (ncr(p, i) * ncr(q, n - i));
}
return sum;
}
// Driver Code
public static void Main()
{
int P = 5, Q = 2, N = 5;
Console.Write(countWays(N, P, Q));
}
}
// This code is contributed by sanjoy_62.
Javascript
10
时间复杂度: O(N 2 )
辅助空间: O(1)