从 P 男性和 Q 女性中选择至少 X 男性和 Y 女性的 N 个人的所有可能方式的计数
给定整数N 、 P 、 Q 、 X和Y ,任务是找出由P 个男人和Q 个女人组成的 N人至少有 X 个男人和Y 个女人的群体的方法数,其中(X + Y ≤ N, X ≤ P 和 Y ≤ Q) 。
例子:
Input: P = 4, Q = 2, N = 5, X = 3, Y = 1
Output: 6
Explanation: Suppose given pool is {m1, m2, m3, m4} and {w1, w2}. Then possible combinations are:
m1 m2 m3 m4 w1
m1 m2 m3 m4 w2
m1 m2 m3 w1 w2
m1 m2 m4 w1 w2
m1 m3 m4 w1 w2
m2 m3 m4 w1 w2
Hence the count is 6.
Input: P = 5, Q = 2, N = 6, X = 4, Y = 1
Output: 7
方法:这个问题是基于组合数学的,我们需要从可用的 P 个男性中选择至少 X 个男性,并且从可用的 Q 个女性中选择至少 Y 个女性,因此选择的总人数是 N。考虑这个例子:
P = 4, Q = 2, N = 5, X = 3, Y = 1.
In this, the possible selections are:(4 men out of 4) * (1 women out of 2) + (3 men out of 4) * (2 woman out of 2)= 4C4 * 2C1 + 4C3 * 2C2
因此对于 P、Q 和 N 的一些一般值,该方法可以可视化为:
where
按照下面提到的步骤来实现它:
- 从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)