📌  相关文章
📜  计数 L 长度的数组可能由前 N 个自然数和每个元素划分下一个元素组成

📅  最后修改于: 2021-09-22 09:41:16             🧑  作者: Mango

给定两个正整数NL ,任务是找到由前N 个自然数组成的L长度数组的数量,使得数组中的每个元素都除以它的下一个元素。

例子:

方法:给定的问题可以通过使用动态规划和埃拉托色尼筛法的思想来解决。请按照以下步骤解决问题:

  • 初始化一个二维数组dp[][] ,其中dp[i][j]表示长度为i且以j结尾的序列的数量,并将dp[0][1]初始化为1
  • 遍历范围[0,L – 1],用变量i和嵌套迭代范围[1,N],用变量j:
    • 如所构造的阵列的下一个元素应该是多个当前元素的,因此迭代范围[J,N]j的递增,并更新DP的值[I + 1] [k]的对数值DP [ i][j] + dp[i + 1][k]
  • 初始化一个变量,说answer0存储数组的结果数量。
  • 迭代范围[1, N]并将dp[L][i]的值添加到变量ans
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Fumction to find the number of
// arrays of length L such that each
// element divides the next element
int numberOfArrays(int n, int l)
{
 
    // Stores the number of sequences
    // of length i that ends with j
    int dp[l + 1][n + 1];
 
    // Initialize 2D array dp[][] as 0
    memset(dp, 0, sizeof dp);
 
    // Base Case
    dp[0][1] = 1;
 
    // Iterate over the range [0, l]
    for (int i = 0; i < l; i++) {
        for (int j = 1; j <= n; j++) {
 
            // Iterate for all multiples
            // of j
            for (int k = j; k <= n; k += j) {
 
                // Incrementing dp[i+1][k]
                // by dp[i][j] as the next
                // element is multiple of j
                dp[i + 1][k] += dp[i][j];
            }
        }
    }
 
    // Stores the number of arrays
    int ans = 0;
 
    for (int i = 1; i <= n; i++) {
 
        // Add all array of length
        // L that ends with i
        ans += dp[l][i];
    }
 
    // Return the resultant count
    return ans;
}
 
// Driver Code
int main()
{
    int N = 2, L = 4;
    cout << numberOfArrays(N, L);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
     
    static int numberOfArrays(int n, int l)
    {
        // Stores the number of sequences
    // of length i that ends with j
    int[][] dp=new int[l + 1][n + 1];
  
     
     
  
    // Base Case
    dp[0][1] = 1;
  
    // Iterate over the range [0, l]
    for (int i = 0; i < l; i++) {
        for (int j = 1; j <= n; j++) {
  
            // Iterate for all multiples
            // of j
            for (int k = j; k <= n; k += j) {
  
                // Incrementing dp[i+1][k]
                // by dp[i][j] as the next
                // element is multiple of j
                dp[i + 1][k] += dp[i][j];
            }
        }
    }
  
    // Stores the number of arrays
    int ans = 0;
  
    for (int i = 1; i <= n; i++) {
  
        // Add all array of length
        // L that ends with i
        ans += dp[l][i];
    }
  
    // Return the resultant count
    return ans;
    }
     
    // Driver Code
    public static void main (String[] args) {
         
        int N = 2, L = 4;
        System.out.println(numberOfArrays(N, L));
    }
}
 
// This code is contributed by unknown2108


Python3
# Python3 program for the above approach
 
# Function to find the number of
# arrays of length L such that each
# element divides the next element
def numberOfArrays(n, l):
     
    # Stores the number of sequences
    # of length i that ends with j
    dp = [[0 for i in range(n + 1)]
             for i in range(l + 1)]
 
    # Initialize 2D array dp[][] as 0a
    # memset(dp, 0, sizeof dp)
 
    # Base Case
    dp[0][1] = 1
 
    # Iterate over the range [0, l]
    for i in range(l):
        for j in range(1, n + 1):
             
            # Iterate for all multiples
            # of j
            for k in range(j, n + 1, j):
                 
                # Incrementing dp[i+1][k]
                # by dp[i][j] as the next
                # element is multiple of j
                dp[i + 1][k] += dp[i][j]
 
    # Stores the number of arrays
    ans = 0
 
    for i in range(1, n + 1):
         
        # Add all array of length
        # L that ends with i
        ans += dp[l][i]
 
    # Return the resultant count
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    N, L = 2, 4
     
    print(numberOfArrays(N, L))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Fumction to find the number of
// arrays of length L such that each
// element divides the next element
static int numberOfArrays(int n, int l)
{
 
    // Stores the number of sequences
    // of length i that ends with j
    int [,]dp = new int[l + 1,n + 1];
 
    // Initialize 2D array dp[][] as 0
    for(int i = 0; i < l + 1; i++){
      for(int j = 0; j < n + 1; j++)
        dp[i, j] = 0;
    }
 
    // Base Case
    dp[0, 1] = 1;
 
    // Iterate over the range [0, l]
    for (int i = 0; i < l; i++) {
        for (int j = 1; j <= n; j++) {
 
            // Iterate for all multiples
            // of j
            for (int k = j; k <= n; k += j) {
 
                // Incrementing dp[i+1][k]
                // by dp[i][j] as the next
                // element is multiple of j
                dp[i + 1,k] += dp[i,j];
            }
        }
    }
 
    // Stores the number of arrays
    int ans = 0;
 
    for (int i = 1; i <= n; i++) {
 
        // Add all array of length
        // L that ends with i
        ans += dp[l,i];
    }
 
    // Return the resultant count
    return ans;
}
 
// Driver Code
public static void Main()
{
    int N = 2, L = 4;
    Console.Write(numberOfArrays(N, L));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
5

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程