📜  大小不超过K的子集总数

📅  最后修改于: 2021-04-27 22:44:18             🧑  作者: Mango

给定数字N(即集合的大小)和数字K ,任务是查找N个元素的集合中最多具有K个元素的子集的数量,即子集的大小小于或。等于K

例子:

方法:

  1. 由于可以由N个项目构成的正好K个元素的子集的数量为( N C K )。因此,对于“至多”,所需的计数为
    \LARGE \sum _{i = 1}^{K} \text{ } ^{N}\textrm{C}_{i} =\text{ } ^{N}\textrm{C}_{1} + ^{N}\textrm{C}_{2} + ^{N}\textrm{C}_{3} + ...+ ^{N}\textrm{C}_{K}
  2. 为了计算N C K的值,使用了二项式系数。请参阅本文以查看其工作方式。
  3. 因此,要获得长度最大为K的必需子集,请运行从1到K的循环,并为i的每个值添加N C i。

下面是上述方法的实现:

C++
// C++ code to find total number of
// Subsets of size at most K
  
#include 
using namespace std;
  
// Function to compute the value
// of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    int C[n + 1][k + 1];
    int i, j;
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++) {
        for (j = 0; j <= min(i, k); 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 calculate sum of
// nCj from j = 1 to k
int count(int n, int k)
{
    int sum = 0;
    for (int j = 1; j <= k; j++) {
  
        // Calling the nCr function
        // for each value of j
        sum = sum + binomialCoeff(n, j);
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int n = 3, k = 2;
    cout << count(n, k);
  
    n = 5, k = 2;
    cout << count(n, k);
    return 0;
}


Java
// Java code to find total number of
// Subsets of size at most K
import java.lang.*;
class GFG
{
  
// Function to compute the value
// of Binomial Coefficient C(n, k)
public static int binomialCoeff(int n, int k)
{
    int[][] C = new int[n + 1][k + 1];
    int i, j;
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++) 
    {
        for (j = 0; j <= Math.min(i, k); 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 calculate sum of
// nCj from j = 1 to k
public static int count(int n, int k)
{
    int sum = 0;
    for (int j = 1; j <= k; j++)
    {
  
        // Calling the nCr function
        // for each value of j
        sum = sum + binomialCoeff(n, j);
    }
  
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    GFG g = new GFG();
    int n = 3, k = 2;
    System.out.print(count(n, k));
  
    int n1 = 5, k1 = 2;
    System.out.print(count(n1, k1));
}
}
  
// This code is contributed by SoumikMondal


Python3
# Python code to find total number of
# Subsets of size at most K
  
# Function to compute the value
# of Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
    C = [[0 for i in range(k + 1)] for j in range(n + 1)];
    i, j = 0, 0;
  
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(n + 1):
        for j in range( min(i, k) + 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 calculate sum of
# nCj from j = 1 to k
def count(n, k):
    sum = 0;
    for j in range(1, k+1):
  
        # Calling the nCr function
        # for each value of j
        sum = sum + binomialCoeff(n, j);
    return sum;
  
# Driver code
if __name__ == '__main__':
    n = 3;
    k = 2;
    print(count(n, k), end="");
  
    n1 = 5;
    k1 = 2;
    print(count(n1, k1));
  
# This code is contributed by 29AjayKumar


C#
// C# code to find total number of
// Subsets of size at most K
using System;
  
class GFG
{
  
    // Function to compute the value
    // of Binomial Coefficient C(n, k)
    public static int binomialCoeff(int n, int k)
    {
        int[,] C = new int[n + 1, k + 1];
        int i, j;
      
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (i = 0; i <= n; i++) 
        {
            for (j = 0; j <= Math.Min(i, k); 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 calculate sum of
    // nCj from j = 1 to k
    public static int count(int n, int k)
    {
        int sum = 0;
        for (int j = 1; j <= k; j++)
        {
      
            // Calling the nCr function
            // for each value of j
            sum = sum + binomialCoeff(n, j);
        }
      
        return sum;
    }
      
    // Driver code
    public static void Main()
    {
  
        int n = 3, k = 2;
        Console.Write(count(n, k));
      
        int n1 = 5, k1 = 2;
        Console.Write(count(n1, k1));
    }
}
  
// This code is contributed by AnkitRai01


输出:
615