📌  相关文章
📜  从 P 男性和 Q 女性中选择至少 X 男性和 Y 女性的 N 个人的所有可能方式的计数

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

从 P 男性和 Q 女性中选择至少 X 男性和 Y 女性的 N 个人的所有可能方式的计数

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

例子:

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

因此对于 P、Q 和 N 的一些一般值,该方法可以可视化为:

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

  • i = X开始迭代一个循环,直到 i = P。
  • 检查(Ni) 是否满足条件(Ni) ≥ Y(Ni) ≤ Q 。如果满足条件,则执行以下操作。
  • 如果我们选择 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,
                        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 += (ncr(p, i) * ncr(q, n - i));
    }
    return sum;
}
 
// Driver code
int main()
{
    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
import java.util.*;
public class GFG
{
 
  // Function to calculate factorial
  static long fact(long f)
  {
    f++;
    long ans = 1;
 
    // Loop to calculate factorial of f
    while (--f > 0)
      ans = ans * f;
    return ans;
  }
 
  // Function to calculate combination nCr
  static long ncr(long n, long r)
  {
    return (fact(n) / (fact(r) * fact(n - r)));
  }
 
  // 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 (long i = x; i <= p; i++) {
      if (n - i >= y && n - i <= q)
        sum += ((int)ncr(p, i) * (int)ncr(q, n - i));
    }
    return sum;
  }
 
  // Driver code
  public static void main(String args[])
  {
    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 Samim Hossain Mondal.


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, 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 += (ncr(p, i) * ncr(q, n - i))   
    return sum
 
# Driver code
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 gfgking


C#
using System;
class GFG
{
   
// Function to calculate factorial
static long fact(long f)
{
    f++;
    long ans = 1;
 
    // Loop to calculate factorial of f
    while (--f > 0)
        ans = ans * f;
    return ans;
}
 
// Function to calculate combination nCr
static long ncr(long n, long r)
{
    return (fact(n) / (fact(r) * fact(n - r)));
}
 
// 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 (long i = x; i <= p; i++) {
        if (n - i >= y && n - i <= q)
            sum += ((int)ncr(p, i) * (int)ncr(q, n - i));
    }
    return sum;
}
 
// Driver code
public static void Main()
{
    int P = 4, Q = 2, N = 5, X = 3, Y = 1;
 
    // Calculate possible ways for given
    // N, P, Q, X and Y
    Console.Write(countWays(N, P, Q, X, Y));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
6

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