📌  相关文章
📜  从 P 男性和 Q 女性中选择至少 X 男性和 Y 女性的 N 人的方法计数 |设置 2

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

从 P 男性和 Q 女性中选择至少 X 男性和 Y 女性的 N 人的方法计数 |设置 2

给定整数N、P、Q、X 和 Y,任务是找出由P 个男人和 Q 个女人组成的N 个人至少有 X 个男人Y 个女人的群体的方法数,其中 (X + Y ≤ N, X ≤ P 和 Y ≤ Q)。

例子:

朴素方法:这个问题是基于组合学的朴素方法的细节已经在这个问题的Set-1中讨论过。

对于 P、Q、N、X 和 Y 的一些一般值,我们可以使用以下公式计算所有可能的方式:

在这种方法中,我们每一步都在计算每种可能方式的价值。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了有效地解决这个问题,我们可以使用帕斯卡三角属性来计算_{r}^{n}\textrm{C}          , IE

这不过是

请按照以下步骤操作:

  • 使用帕斯卡三角形预先计算组合的值。
  • 开始从i = Xi = P迭代一个循环,并为每次迭代执行以下操作。
  • 检查(Ni) ≥ Y(Ni) ≤ Q是否。
  • 如果满足条件,则计算i men(Ni) women的可能方式,否则,跳过该步骤。
  • 计数与总路数相加。
  • 返回总计数作为您的答案。

下面是该方法的实现:

C++
#include 
using namespace std;
 
long long int pascal[31][31];
 
// Function to calculate the pascal triangle
void pascalTriangle()
{
    pascal[0][0] = 1;
    pascal[1][0] = 1;
    pascal[1][1] = 1;
 
    // Loop to calculate values of
    // pascal triangle
    for (int i = 2; i < 31; i++) {
        pascal[i][0] = 1;
        for (int j = 1; j < i; j++)
            pascal[i][j]
                = pascal[i - 1][j]
                  + pascal[i - 1][j - 1];
        pascal[i][i] = 1;
    }
}
 
// Function to calculate the number of ways
long long int countWays(int n, int p,
                        int q, int x,
                        int y)
{
 
    // Variable to store the answer
    long long int sum = 0;
 
    // Loop to calculate the number of ways
    for (long long int i = x; i <= p; i++) {
        if (n - i >= y && n - i <= q)
            sum += pascal[p][i]
                   * pascal[q][n - i];
    }
    return sum;
}
 
// Driver code
int main()
{
    pascalTriangle();
 
    int P = 4, Q = 2, N = 5, X = 3, Y = 1;
 
    // Calculate possible ways for given
    // N, P, Q, X and Y
    cout << countWays(N, P, Q, X, Y)
         << endl;
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
class GFG {
 
    static long pascal[][] = new long[31][31];
 
    // Function to calculate the pascal triangle
    static void pascalTriangle()
    {
        pascal[0][0] = 1;
        pascal[1][0] = 1;
        pascal[1][1] = 1;
 
        // Loop to calculate values of
        // pascal triangle
        for (int i = 2; i < 31; i++) {
            pascal[i][0] = 1;
            for (int j = 1; j < i; j++)
                pascal[i][j] = pascal[i - 1][j]
                               + pascal[i - 1][j - 1];
            pascal[i][i] = 1;
        }
    }
 
    // Function to calculate the number of ways
    static long countWays(int n, int p, int q, int x, int y)
    {
 
        // Variable to store the answer
        long sum = 0;
 
        // Loop to calculate the number of ways
        for (int i = x; i <= p; i++) {
            if (n - i >= y && n - i <= q)
                sum += pascal[p][i] * pascal[q][n - i];
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        pascalTriangle();
        int P = 4, Q = 2, N = 5, X = 3, Y = 1;
 
        // Calculate possible ways for given
        // N, P, Q, X and Y
        System.out.println(countWays(N, P, Q, X, Y));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
pascal = [[0 for i in range(31)] for j in range(31)]
 
# Function to calculate the pascal triangle
def pascalTriangle():
  pascal[0][0] = 1;
  pascal[1][0] = 1;
  pascal[1][1] = 1;
 
  # Loop to calculate values of
  # pascal triangle
  for i in range(2, 31):
    pascal[i][0] = 1;
    for j in range(i):
      pascal[i][j] = pascal[i - 1][j] + pascal[i - 1][j - 1];
    pascal[i][i] = 1;
 
# Function to calculate the number of ways
def countWays(n, p, q, x, y):
 
  # Variable to store the answer
  sum = 0;
 
  # Loop to calculate the number of ways
  for i in range(x, p + 1):
    if (n - i >= y and n - i <= q):
      sum += pascal[p][i] * pascal[q][n - i];
  return sum;
 
# Driver code
pascalTriangle();
 
P = 4
Q = 2
N = 5
X = 3
Y = 1;
 
# Calculate possible ways for given
# N, P, Q, X and Y
print(countWays(N, P, Q, X, Y))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code for the above approach
using System;
 
class GFG{
 
static long [,]pascal = new long[31, 31];
 
// Function to calculate the pascal triangle
static void pascalTriangle()
{
    pascal[0, 0] = 1;
    pascal[1, 0] = 1;
    pascal[1, 1] = 1;
 
    // Loop to calculate values of
    // pascal triangle
    for(int i = 2; i < 31; i++)
    {
        pascal[i, 0] = 1;
        for(int j = 1; j < i; j++)
            pascal[i, j] = pascal[i - 1, j] +
                           pascal[i - 1, j - 1];
        pascal[i, i] = 1;
    }
}
 
// Function to calculate the number of ways
static long countWays(int n, int p, int q, int x, int y)
{
     
    // Variable to store the answer
    long sum = 0;
 
    // Loop to calculate the number of ways
    for(int i = x; i <= p; i++)
    {
        if (n - i >= y && n - i <= q)
            sum += pascal[p, i] * pascal[q, n - i];
    }
    return sum;
}
 
// Driver code
public static void Main(String[] args)
{
    pascalTriangle();
     
    int P = 4, Q = 2, N = 5, X = 3, Y = 1;
 
    // Calculate possible ways for given
    // N, P, Q, X and Y
    Console.WriteLine(countWays(N, P, Q, X, Y));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
6

时间复杂度: O(N)
辅助空间: O(N 2 )