📜  使用允许重复的最大自然数求和N的方法

📅  最后修改于: 2021-06-25 12:12:50             🧑  作者: Mango

给定两个整数NK ,任务是找到将N表示为[1,K]范围内的正整数之和的方式总数,其中每个整数可以多次选择。

例子:

天真的方法:解决给定问题的最简单方法是生成选择[1,K]范围内的整数的所有可能组合,并计算总和为N的那些组合。

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

高效方法:以上方法具有重叠子问题和最佳子结构。因此,为了进行优化,需要基于以下观察来执行动态编程:

  • 考虑到dp [i]将表示i的方式总数存储为[1,K]范围内的整数之和,则状态转换可以定义为:
    • 对于i在范围[1,K]和在范围内的每个f] [1,N]
    • DP [J]的值等于(DP [J] + DP [J – I]),对于所有j≥我

请按照以下步骤解决问题:

  • 初始化一个数组,例如dp [] ,所有元素都为0 ,以存储所有递归状态。
  • dp [0]初始化为1
  • 现在,使用变量i遍历[1,K]范围并执行以下步骤:
    • 遍历范围[1,N],使用变量j,并更新为DP [J] + DP [J – I] DP [j]的值,如果j≥我
  • 完成上述步骤后,打印dp [N]的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
int NumberOfways(int N, int K)
{
 
    // Initialize a list
    vector dp(N + 1, 0);
   
    // Update dp[0] to 1
    dp[0] = 1;
 
    // Iterate over the range [1, K + 1]
    for (int row = 1; row < K + 1; row++)
    {
 
        // Iterate over the range [1, N + 1]
        for (int col = 1; col < N + 1; col++)
        {
 
            // If col is greater
            // than or equal to row
            if (col >= row)
               
                // Update current
                // dp[col] state
                dp[col] = dp[col] + dp[col - row];
          }
    }
 
    // Return the total number of ways
    return(dp[N]);
}
 
// Driver Code
int main()
{
  int N = 8;
  int K = 2;
 
  cout << (NumberOfways(N, K));
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
static int NumberOfways(int N, int K)
{
     
    // Initialize a list
    int[] dp = new int[N + 1];
   
    // Update dp[0] to 1
    dp[0] = 1;
 
    // Iterate over the range [1, K + 1]
    for(int row = 1; row < K + 1; row++)
    {
 
        // Iterate over the range [1, N + 1]
        for(int col = 1; col < N + 1; col++)
        {
             
            // If col is greater
            // than or equal to row
            if (col >= row)
               
                // Update current
                // dp[col] state
                dp[col] = dp[col] + dp[col - row];
          }
    }
 
    // Return the total number of ways
    return(dp[N]);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given inputs
    int N = 8;
    int K = 2;
     
    System.out.println(NumberOfways(N, K));
}
}
 
// This code is contributed by offbeat


Python
# Python program for the above approach
 
# Function to find the total number of
# ways to represent N as the sum of
# integers over the range [1, K]
def NumberOfways(N, K):
   
    # Initialize a list
    dp = [0] * (N + 1)
     
    # Update dp[0] to 1
    dp[0] = 1
     
    # Iterate over the range [1, K + 1]
    for row in range(1, K + 1):
       
        # Iterate over the range [1, N + 1]
        for col in range(1, N + 1):
           
            # If col is greater
            # than or equal to row
            if (col >= row):
               
                # Update current
                # dp[col] state
                dp[col] = dp[col] + dp[col - row]
                 
                 
    # Return the total number of ways
    return(dp[N])
 
# Driver Code
 
N = 8
K = 2
 
print(NumberOfways(N, K))


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to find the total number of
    // ways to represent N as the sum of
    // integers over the range [1, K]
    static int NumberOfways(int N, int K)
    {
 
        // Initialize a list
        int[] dp = new int[(N + 1)];
 
        // Update dp[0] to 1
        dp[0] = 1;
 
        // Iterate over the range [1, K + 1]
        for (int row = 1; row < K + 1; row++) {
 
            // Iterate over the range [1, N + 1]
            for (int col = 1; col < N + 1; col++) {
 
                // If col is greater
                // than or equal to row
                if (col >= row)
 
                    // Update current
                    // dp[col] state
                    dp[col] = dp[col] + dp[col - row];
            }
        }
 
        // Return the total number of ways
        return (dp[N]);
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 8;
        int K = 2;
 
        Console.WriteLine(NumberOfways(N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
5

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