📌  相关文章
📜  从 P 个男孩和 Q 个女孩中选择 N 个包含至少 4 个男孩和 1 个女孩的人的方法计数

📅  最后修改于: 2022-05-13 01:56:05.550000             🧑  作者: Mango

从 P 个男孩和 Q 个女孩中选择 N 个包含至少 4 个男孩和 1 个女孩的人的方法计数

给定整数NPQ ,任务是从P 个男孩Q 个女孩中找出由N 个人组成的一组至少有 4 个男孩1 个女孩的方法数。

例子:

方法:这个问题是基于组合数学的,我们需要从可用的1个男孩中选择至少4 个男孩,并且从可用的Q个女孩中选择至少Y个女孩,因此选择的总人数是 N。
考虑这个例子:

因此,对于PQN的一些一般值,该方法可以可视化为:

_{4}^{P}\textrm{C} \ast _{N-4}^{Q}\textrm{C} + _{5}^{P}\textrm{C} \ast _{N-5}^{Q}\textrm{C} + . . . + _{N-2}^{P}\textrm{C} \ast _{2}^{Q}\textrm{C} + _{N-1}^{P}\textrm{C} \ast _{1}^{Q}\textrm{C}
在哪里
_{r}^{n}\textrm{C} = \frac{n!}{r!*(n-r)!}

按照下面提到的步骤来实现它:

  • i = 4开始迭代循环直到i = P
  • 在每次迭代中,如果我们选择i 个男孩(Ni) 个女孩,使用组合计算可能方式的数量
    _{i}^{P}\textrm{C} \ast _{N-i}^{Q}\textrm{C}
  • 将每次迭代的可能值添加为方式的总数
  • 最后返回总计算方式。

下面是该方法的实现:

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)