给定三个数字(x、y 和 z),分别表示第一组、第二组和第三组的人数。我们可以通过从第一组、第二组和第三组中选择人员来组成组,使得以下条件不无效。
- 必须从每个组中至少选择一个人。
- 从第一组中选出的人数必须至少比从第三组中选出的人数多一个。
任务是找到形成不同组的方法的数量。
例子:
Input: x = 3, y = 2, z = 1
Output: 9
Lets say x has people (a, b, c)
Y has people (d, e)
Z has people (f)
Then the 9 ways are {a, b, d, f}, {a, b, e, f}, {a, c, d, f}, {a, c, e, f},
{b, c, d, f}, {b, c, e, f}, {a, b, c, d, f}, {a, b, c, e, f} and
{a, b, c, d, e, f}
Input: x = 4, y = 2, z = 1
Output: 27
这个问题可以用组合学来解决。需要填补三个职位(就来自不同群体的人而言)。第一个必须填充一个比第二个位置大一个或多个的数字。第三个可以填充任意数字。我们知道如果我们需要用 N 个人来填补 k 个职位,那么这样做的方法的数量是 .因此,可以按照以下步骤来解决上述问题。
- 第二个位置可以填 i=1 到 i=y 的人。
- 第一个位置可以填充 j = i+1 到 j = x 人。
- 第三个位置可以由任意数量的 k = 1 到 k = z 人填补。
- 因此,常见的事情是用 k 个人填充第三个位置。因此,我们可以将那部分视为常见的。
- 运行两个循环(i 和 j)分别填充第二个位置和第一个位置。
- 填补职位的方式数是 * .
- 在计算了填补这两个位置的所有方法之后,我们可以简单地乘以总和 + + … 因为这是两者的共同部分。
可以使用动态规划预先计算以降低时间复杂度。此处讨论该方法。
下面是上述方法的实现。
C++
// C++ program to find the number of
// ways to form the group of peopls
#include
using namespace std;
int C[1000][1000];
// Function to pre-compute the
// Combination using DP
void binomialCoeff(int n)
{
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++) {
for (j = 0; j <= i; j++) {
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previously
// stored values
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
// return C[n][k];
}
// Function to find the number of ways
int numberOfWays(int x, int y, int z)
{
// Function to pre-compute
binomialCoeff(max(x, max(y, z)));
// Sum the Zci
int sum = 0;
for (int i = 1; i <= z; i++) {
sum = (sum + C[z][i]);
}
// Iterate for second position
int sum1 = 0;
for (int i = 1; i <= y; i++) {
// Iterate for first position
for (int j = i + 1; j <= x; j++) {
sum1 = (sum1 + (C[y][i] * C[x][j]));
}
}
// Multiply the common Combination value
sum1 = (sum * sum1);
return sum1;
}
// Driver Code
int main()
{
int x = 3;
int y = 2;
int z = 1;
cout << numberOfWays(x, y, z);
return 0;
}
Java
// Java program to find the number of
// ways to form the group of peopls
class GFG
{
static int C[][] = new int [1000][1000];
// Function to pre-compute the
// Combination using DP
static void binomialCoeff(int n)
{
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= i; j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previously
// stored values
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
// return C[n][k];
}
// Function to find the number of ways
static int numberOfWays(int x, int y, int z)
{
// Function to pre-compute
binomialCoeff(Math.max(x, Math.max(y, z)));
// Sum the Zci
int sum = 0;
for (int i = 1; i <= z; i++)
{
sum = (sum + C[z][i]);
}
// Iterate for second position
int sum1 = 0;
for (int i = 1; i <= y; i++)
{
// Iterate for first position
for (int j = i + 1; j <= x; j++)
{
sum1 = (sum1 + (C[y][i] * C[x][j]));
}
}
// Multiply the common Combination value
sum1 = (sum * sum1);
return sum1;
}
// Driver Code
public static void main(String args[])
{
int x = 3;
int y = 2;
int z = 1;
System.out.println(numberOfWays(x, y, z));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the number of
# ways to form the group of peopls
C = [[0 for i in range(1000)]
for i in range(1000)]
# Function to pre-compute the
# Combination using DP
def binomialCoeff(n):
i, j = 0, 0
# Calculate value of Binomial Coefficient
# in bottom up manner
for i in range(n + 1):
for j in range(i + 1):
# Base Cases
if (j == 0 or j == i):
C[i][j] = 1
# Calculate value using previously
# stored values
else:
C[i][j] = C[i - 1][j - 1] + \
C[i - 1][j]
# return C[n][k]
# Function to find the number of ways
def numberOfWays(x, y, z):
# Function to pre-compute
binomialCoeff(max(x, max(y, z)))
# Sum the Zci
sum = 0
for i in range(1, z + 1):
sum = (sum + C[z][i])
# Iterate for second position
sum1 = 0
for i in range(1, y + 1):
# Iterate for first position
for j in range(i + 1, x + 1):
sum1 = (sum1 + (C[y][i] * C[x][j]))
# Multiply the common Combination value
sum1 = (sum * sum1)
return sum1
# Driver Code
x = 3
y = 2
z = 1
print(numberOfWays(x, y, z))
# This code is contributed by Mohit Kumar
C#
// C# program to find the number of
// ways to form the group of peopls
using System;
class GFG
{
static int [,]C = new int [1000,1000];
// Function to pre-compute the
// Combination using DP
static void binomialCoeff(int n)
{
int i, j;
// Calculate value of Binomial Coefficient
// in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= i; j++)
{
// Base Cases
if (j == 0 || j == i)
C[i,j] = 1;
// Calculate value using previously
// stored values
else
C[i,j] = C[i - 1,j - 1] + C[i - 1,j];
}
}
// return C[n,k];
}
// Function to find the number of ways
static int numberOfWays(int x, int y, int z)
{
// Function to pre-compute
binomialCoeff(Math.Max(x, Math.Max(y, z)));
// Sum the Zci
int sum = 0;
for (int i = 1; i <= z; i++)
{
sum = (sum + C[z,i]);
}
// Iterate for second position
int sum1 = 0;
for (int i = 1; i <= y; i++)
{
// Iterate for first position
for (int j = i + 1; j <= x; j++)
{
sum1 = (sum1 + (C[y,i] * C[x,j]));
}
}
// Multiply the common Combination value
sum1 = (sum * sum1);
return sum1;
}
// Driver Code
public static void Main(String []args)
{
int x = 3;
int y = 2;
int z = 1;
Console.WriteLine(numberOfWays(x, y, z));
}
}
// This code is contributed by Rajput-Ji
Javascript
9
时间复杂度: O(K * K ),其中 K 是 (x, y 和 z) 的最大值。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。