📜  将N写入K个非负整数之和的方式数量

📅  最后修改于: 2021-04-27 19:16:57             🧑  作者: Mango

给定两个正整数NK ,任务是将写入N的方式的数量计算为K个非负整数的总和。

例子:

方法:可以使用动态编程解决此问题。步骤如下:

  1. 将2D数组初始化为dp [K + 1] [N + 1] ,其中行对应于我们选择的元素数,列对应于相应的总和。
  2. 在上面的表dp [] []中,以sum为K开始填充第一行和第一列。
  3. 假设我们到达第i行和第j列,即我们可以选择i个元素,我们需要求和j。要计算直到dp [i] [j]的路数,请选择第一个(i – 1)个元素,然后选择下一个(j – x) ,其中x是第一个(i – 1)个元素的总和。
  4. 重复上述步骤以填充dp [] []数组。
  5. dp [n] [m]将给出所需的结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
int countWays(int n, int m)
{
  
    // Initialise dp[][] array
    int dp[m + 1][n + 1];
  
    // Only 1 way to choose the value
    // with sum K
    for (int i = 0; i <= n; i++) {
        dp[1][i] = 1;
    }
  
    // Initialise sum
    int sum;
  
    for (int i = 2; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            sum = 0;
  
            // Count the ways from previous
            // states
            for (int k = 0; k <= j; k++) {
                sum += dp[i - 1][k];
            }
  
            // Update the sum
            dp[i][j] = sum;
        }
    }
  
    // Return the final count of ways
    return dp[m][n];
}
  
// Driver Code
int main()
{
    int N = 2, K = 3;
  
    // Function call
    cout << countWays(N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
  
    // Initialise dp[][] array
    int [][]dp = new int[m + 1][n + 1];
  
    // Only 1 way to choose the value
    // with sum K
    for(int i = 0; i <= n; i++)
    {
       dp[1][i] = 1;
    }
  
    // Initialise sum
    int sum;
  
    for(int i = 2; i <= m; i++) 
    {
       for(int j = 0; j <= n; j++)
       {
          sum = 0;
            
          // Count the ways from previous
          // states
          for(int k = 0; k <= j; k++)
          {
             sum += dp[i - 1][k];
          }
            
          // Update the sum
          dp[i][j] = sum;
       }
    }
  
    // Return the final count of ways
    return dp[m][n];
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 2, K = 3;
  
    // Function call
    System.out.print(countWays(N, K));
}
}
  
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
  
# Function to count the number of ways
# to write N as sum of k non-negative
# integers
def countWays(n, m):
  
    # Initialise dp[][] array
    dp = [[ 0 for i in range(n + 1)]
              for i in range(m + 1)]
                
    # Only 1 way to choose the value
    # with sum K
    for i in range(n + 1):
        dp[1][i] = 1
  
    # Initialise sum
    sum = 0
  
    for i in range(2, m + 1):
        for j in range(n + 1):
            sum = 0
  
            # Count the ways from previous
            # states
            for k in range(j + 1):
                sum += dp[i - 1][k]
  
            # Update the sum
            dp[i][j] = sum
  
    # Return the final count of ways
    return dp[m][n]
  
# Driver Code
if __name__ == '__main__':
    N = 2
    K = 3
  
    # Function call
    print(countWays(N, K))
  
# This code is contributed by Mohit Kumar


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
  
    // Initialise [,]dp array
    int [,]dp = new int[m + 1, n + 1];
  
    // Only 1 way to choose the value
    // with sum K
    for(int i = 0; i <= n; i++)
    {
       dp[1, i] = 1;
    }
  
    // Initialise sum
    int sum;
  
    for(int i = 2; i <= m; i++) 
    {
       for(int j = 0; j <= n; j++)
       {
          sum = 0;
            
          // Count the ways from previous
          // states
          for(int k = 0; k <= j; k++)
          {
             sum += dp[i - 1, k];
          }
            
          // Update the sum
          dp[i, j] = sum;
       }
    }
  
    // Return the readonly count of ways
    return dp[m, n];
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 2, K = 3;
  
    // Function call
    Console.Write(countWays(N, K));
}
}
  
// This code is contributed by gauravrajput1


C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
int countWays(int n, int m)
{
    // Initialise dp[][] array
    int dp[m + 1][n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++) {
        dp[1][i] = 1;
        if (i != 0) {
            dp[1][i] += dp[1][i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
  
            // Condition for first column
            if (j == 0) {
                dp[i][j] = dp[i - 1][j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else {
                dp[i][j] = dp[i - 1][j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) {
                    return dp[i][j];
                }
  
                // Update at current index
                dp[i][j] += dp[i][j - 1];
            }
        }
    }
}
  
// Driver Code
int main()
{
    int N = 2, K = 3;
  
    // Function call
    cout << countWays(N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
    // Initialise dp[][] array
    int [][]dp = new int[m + 1][n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++)
    {
        dp[1][i] = 1;
        if (i != 0)
        {
            dp[1][i] += dp[1][i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) 
    {
        for (int j = 0; j <= n; j++) 
        {
  
            // Condition for first column
            if (j == 0) 
            {
                dp[i][j] = dp[i - 1][j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else 
            {
                dp[i][j] = dp[i - 1][j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) 
                {
                    return dp[i][j];
                }
  
                // Update at current index
                dp[i][j] += dp[i][j - 1];
            }
        }
    }
    return Integer.MIN_VALUE;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 2, K = 3;
  
    // Function call
    System.out.print(countWays(N, K));
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach
  
# Function to count the number of ways
# to write N as sum of k non-negative
# integers
def countWays(n, m):
      
    # Initialise dp[][] array
    dp = [[0 for i in range(n + 1)]
             for j in range(m + 1)]
      
    # Fill the dp[][] with sum = m
    for i in range(n + 1):
        dp[1][i] = 1
        if (i != 0):
            dp[1][i] += dp[1][i - 1]
      
    # Iterate the dp[][] to fill the
    # dp[][] array
    for i in range(2, m + 1):
        for j in range(n + 1):
              
            # Condition for first column
            if (j == 0):
                dp[i][j] = dp[i - 1][j]
              
            # Else fill the dp[][] with
            # sum till (i, j)
            else:
                dp[i][j] = dp[i - 1][j]
                  
                # If reach the end, then
                # return the value
                if (i == m and j == n):
                    return dp[i][j]
                  
                # Update at current index
                dp[i][j] += dp[i][j - 1]
                  
# Driver Code
N = 2
K = 3
  
# Function call
print(countWays(N, K))
  
# This code is contributed by ShubhamCoder


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
    // Initialise dp[][] array
    int [,]dp = new int[m + 1, n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++)
    {
        dp[1, i] = 1;
        if (i != 0)
        {
            dp[1, i] += dp[1, i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) 
    {
        for (int j = 0; j <= n; j++) 
        {
  
            // Condition for first column
            if (j == 0) 
            {
                dp[i, j] = dp[i - 1, j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else
            {
                dp[i, j] = dp[i - 1, j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) 
                {
                    return dp[i, j];
                }
  
                // Update at current index
                dp[i, j] += dp[i, j - 1];
            }
        }
    }
    return Int32.MinValue;
}
  
// Driver Code
public static void Main()
{
    int N = 2, K = 3;
  
    // Function call
    Console.Write(countWays(N, K));
}
}
  
// This code is contributed by Code_Mech


输出:
6


时间复杂度: O(K * N 2 )
辅助空间复杂度: O(N * K)

优化方法:计算总和然后存储计数的想法增加了时间复杂度。我们可以通过将总和存储在上面的dp [] []表中来减少它。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
int countWays(int n, int m)
{
    // Initialise dp[][] array
    int dp[m + 1][n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++) {
        dp[1][i] = 1;
        if (i != 0) {
            dp[1][i] += dp[1][i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
  
            // Condition for first column
            if (j == 0) {
                dp[i][j] = dp[i - 1][j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else {
                dp[i][j] = dp[i - 1][j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) {
                    return dp[i][j];
                }
  
                // Update at current index
                dp[i][j] += dp[i][j - 1];
            }
        }
    }
}
  
// Driver Code
int main()
{
    int N = 2, K = 3;
  
    // Function call
    cout << countWays(N, K);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
    // Initialise dp[][] array
    int [][]dp = new int[m + 1][n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++)
    {
        dp[1][i] = 1;
        if (i != 0)
        {
            dp[1][i] += dp[1][i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) 
    {
        for (int j = 0; j <= n; j++) 
        {
  
            // Condition for first column
            if (j == 0) 
            {
                dp[i][j] = dp[i - 1][j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else 
            {
                dp[i][j] = dp[i - 1][j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) 
                {
                    return dp[i][j];
                }
  
                // Update at current index
                dp[i][j] += dp[i][j - 1];
            }
        }
    }
    return Integer.MIN_VALUE;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 2, K = 3;
  
    // Function call
    System.out.print(countWays(N, K));
}
}
  
// This code is contributed by sapnasingh4991

Python3

# Python3 program for the above approach
  
# Function to count the number of ways
# to write N as sum of k non-negative
# integers
def countWays(n, m):
      
    # Initialise dp[][] array
    dp = [[0 for i in range(n + 1)]
             for j in range(m + 1)]
      
    # Fill the dp[][] with sum = m
    for i in range(n + 1):
        dp[1][i] = 1
        if (i != 0):
            dp[1][i] += dp[1][i - 1]
      
    # Iterate the dp[][] to fill the
    # dp[][] array
    for i in range(2, m + 1):
        for j in range(n + 1):
              
            # Condition for first column
            if (j == 0):
                dp[i][j] = dp[i - 1][j]
              
            # Else fill the dp[][] with
            # sum till (i, j)
            else:
                dp[i][j] = dp[i - 1][j]
                  
                # If reach the end, then
                # return the value
                if (i == m and j == n):
                    return dp[i][j]
                  
                # Update at current index
                dp[i][j] += dp[i][j - 1]
                  
# Driver Code
N = 2
K = 3
  
# Function call
print(countWays(N, K))
  
# This code is contributed by ShubhamCoder

C#

// C# program for the above approach
using System;
class GFG{
  
// Function to count the number of ways
// to write N as sum of k non-negative
// integers
static int countWays(int n, int m)
{
    // Initialise dp[][] array
    int [,]dp = new int[m + 1, n + 1];
  
    // Fill the dp[][] with sum = m
    for (int i = 0; i <= n; i++)
    {
        dp[1, i] = 1;
        if (i != 0)
        {
            dp[1, i] += dp[1, i - 1];
        }
    }
  
    // Iterate the dp[][] to fill the
    // dp[][] array
    for (int i = 2; i <= m; i++) 
    {
        for (int j = 0; j <= n; j++) 
        {
  
            // Condition for first column
            if (j == 0) 
            {
                dp[i, j] = dp[i - 1, j];
            }
  
            // Else fill the dp[][] with
            // sum till (i, j)
            else
            {
                dp[i, j] = dp[i - 1, j];
  
                // If reach the end, then
                // return the value
                if (i == m && j == n) 
                {
                    return dp[i, j];
                }
  
                // Update at current index
                dp[i, j] += dp[i, j - 1];
            }
        }
    }
    return Int32.MinValue;
}
  
// Driver Code
public static void Main()
{
    int N = 2, K = 3;
  
    // Function call
    Console.Write(countWays(N, K));
}
}
  
// This code is contributed by Code_Mech
输出:
6


时间复杂度: O(K * N)
辅助空间复杂度: O(N * K)