从 P Boys 和 Q Girls 中选择包含至少 4 个男孩和 1 个女孩的 N 人的方法计数 |设置 2
给定整数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
朴素方法:这个问题是基于组合学的,朴素方法的细节已经在这个问题的Set-1中讨论过。
对于 P、Q 和 N 的一些一般值,我们可以使用以下公式计算所有可能的方式:
where
在这种方法中,我们每一步都在计算每种可能方式的价值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了有效地解决这个问题,我们可以使用帕斯卡三角属性来计算 , IE
1
1 1
1 2 1
1 3 3 1
.
.
.
这不过是
.
.
.
请按照以下步骤操作:
- 使用帕斯卡三角形预先计算组合的值。
- 开始从i = 4到i = P迭代一个循环,并为每次迭代执行以下操作。
- 检查(Ni) ≥ 1和(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)
{
// Variable to store the answer
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 += pascal[p][i]
* pascal[q][n - i];
}
return sum;
}
// Driver code
int main()
{
pascalTriangle();
int P = 5, Q = 2, N = 5;
// Calculate possible ways for given
// N, P, and Q
cout << countWays(N, P, Q) << endl;
return 0;
}
Java
import java.util.*;
public 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] = (int)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)
{
// Variable to store the answer
long sum = 0;
// Loop to calculate the number of ways
for (int i = 4; i <= p; i++) {
if (n - i >= 1 && n - i <= q) {
sum += (int)pascal[p][i]
* (int)pascal[q][n - i];
}
}
return sum;
}
// Driver code
public static void main(String args[])
{
pascalTriangle();
int P = 5, Q = 2, N = 5;
// Calculate possible ways for given
// N, P, and Q
System.out.print(countWays(N, P, Q));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program for the above approach
import numpy as np
pascal = np.zeros((31,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(1, 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) :
# Variable to store the answer
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 += pascal[p][i] * pascal[q][n - i];
return int(sum);
# Driver code
if __name__ == "__main__" :
pascalTriangle();
P = 5; Q = 2; N = 5;
# Calculate possible ways for given
# N, P, and Q
print(countWays(N, P, Q));
# This code is contributed by AnkThon
C#
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] = (int)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)
{
// Variable to store the answer
long sum = 0;
// Loop to calculate the number of ways
for (int i = 4; i <= p; i++) {
if (n - i >= 1 && n - i <= q) {
sum += (int)pascal[p, i]
* (int)pascal[q, n - i];
}
}
return sum;
}
// Driver code
public static void Main()
{
pascalTriangle();
int P = 5, Q = 2, N = 5;
// Calculate possible ways for given
// N, P, and Q
Console.Write(countWays(N, P, Q));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
10
时间复杂度: O(N)
辅助空间: O(N 2 )