📜  将N表示为K个非零整数之和的不同方法

📅  最后修改于: 2021-06-25 13:29:41             🧑  作者: Mango

给定N和K。任务是找出将N表示为K个非零整数之和的多少种不同方式。

例子:

解决问题的方法是观察序列并使用组合来解决问题。要获得数字N,需要N 1,将N 1的总和将得到N。这个问题允许仅使用K个整数来生成N。

观察:

Let's take N = 5 and K = 3, then all 
possible combinations of K numbers are: ( 1, 1, 3 )
                                        ( 1, 3, 1 )
                                        ( 3, 1, 1 )
                                        ( 1, 2, 2 )
                                        ( 2, 2, 1 )
                                        ( 2, 1, 2 )

The above can be rewritten as: ( 1, 1, 1 + 1 + 1 )
                               ( 1, 1 + 1 + 1, 1 )
                               ( 1 + 1 + 1, 1, 1 )
                               ( 1, 1 + 1, 1 + 1 )
                               ( 1 + 1, 1 + 1, 1 )
                               ( 1 + 1, 1, 1 + 1 )

从上面可以得出结论,在N 1个中,必须在N 1个之间放置k-1个逗号,其余的位置用“ +”号填充。答案是将k-1个逗号和在其余位置加上“ +”号的所有组合。因此,通常来说,对于N,所有1之间都将有N-1个空格,并且从中选择k-1并将逗号放在这1个之间。在其余1个之间,放置“ +”号。所以从N-1中选择K-1个对象的方法是N-1 \choose K-1 。动态编程方法用于计算N-1 \choose K-1

下面是上述方法的实现:

C++
// CPP program to calculate Different ways to
// represent N as sum of K non-zero integers.
#include 
using namespace std;
  
// Returns 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];
}
  
// Driver Code
int main()
{
    int n = 5, k = 3;
    cout << "Total number of different ways are "
         << binomialCoeff(n - 1, k - 1);
    return 0;
}


Java
// Java program to calculate 
// Different ways to represent
// N as sum of K non-zero integers.
import java.io.*;
  
class GFG 
{
  
// Returns value of Binomial
// Coefficient C(n, k)
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];
}
  
// Driver Code
public static void main (String[] args)
{
    int n = 5, k = 3;
    System.out.println( "Total number of " + 
                     "different ways are " +
                        binomialCoeff(n - 1, 
                                      k - 1));
}
}
  
// This code is contributed
// by anuj_67.


Python3
# python 3 program to calculate Different ways to
# represent N as sum of K non-zero integers.
  
# Returns value of Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
    C = [[0 for i in range(k+1)]for i in range(n+1)]
  
    # Calculate value of Binomial Coefficient in bottom up manner
    for i in range(0,n+1,1):
        for j in range(0,min(i, k)+1,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]
  
# Driver Code
if __name__ == '__main__':
    n = 5
    k = 3
    print("Total number of different ways are",binomialCoeff(n - 1, k - 1))
      
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to calculate 
// Different ways to represent
// N as sum of K non-zero integers.
using System;
  
class GFG 
{
  
// Returns value of Binomial
// Coefficient C(n, k)
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];
}
  
// Driver Code
public static void Main ()
{
    int n = 5, k = 3;
    Console.WriteLine( "Total number of " + 
                    "different ways are " +
                       binomialCoeff(n - 1, 
                                   k - 1));
}
}
  
// This code is contributed
// by anuj_67.


PHP


输出:
Total number of different ways are 6

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。