Count of sequence of length K in range [1, N] where every element is a multiple of its previous one

• Last Updated : 06 Dec, 2021

Given two integers N and K, the task is to find the count of sequences of K elements from the range [1, N] where every element is a multiple of the previous element.

Example:

Input: N = 4, K = 3
Output: 13
Explanation: The sequences that can be made from the integers 1, 2, 3, 4 having 3 elements are: {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {1, 1, 2}, {1, 2, 2}, {1, 2, 4}, {1, 1, 3}, {1, 3, 3}, {1, 1, 4}, {1, 4, 4}, {2, 2, 4}, and {2, 4, 4}.

Input: N = 9, K = 5
Output: 111

Approach: The given problem can be solved using recursion with memoization. Follow the below steps to solve the problem:

• Create a 2D array dp[][] which stores the memorized states where dp[i][j] represents the count of sequences of length i having j as their first element.
• Create a recursive function countSequenceUtil(), that takes the length of the sequence and the starting element as arguments, sets the next element as a multiple of the current element, and recursively calls for the remaining sequence.
• Store the answer for the calculated states in the dp[][] array and if for some state the value is already calculated, return it.
• Create a function countSequence() which iterates through all the possible starting elements of the sequence and calls the recursive function to calculate the sequences of K elements with that starting element.
• Maintain the sum of the calculated count for each starting element in a variable ans which is the required value.

Below is the implementation of the above approach:

C++

 // C++ implementation for the above approach#include using namespace std; // Initialize the dp matrixint static dp; // Function to find the count of sequences of K// elements with first element as m where every// element is a multiple of the previous oneint countSequenceUtil(int k, int m, int n){    // Base case    if (k == 1) {        return 1;    }     // If the value already exists    // in the DP then return it    if (dp[k][m] != -1) {        return dp[k][m];    }     // Variable to store the count    int res = 0;     for (int i = 1; i <= (n / m); i++) {         // Recursive Call        res += countSequenceUtil(k - 1,                                 m * i, n);    }     // Store the calculated    // answer and return it    return dp[k][m] = res;} // Function to find count of sequences of K// elements in the range [1, n] where every// element is a multiple of the previous oneint countSequence(int N, int K){    // Initializing all values    // of dp with -1    memset(dp, -1, sizeof(dp));     // Variable to store    // the total count    int ans = 0;     // Iterate from 1 to N    for (int i = 1; i <= N; i++) {        ans += countSequenceUtil(K, i, N);    }     // Return ans    return ans;} // Driver Codeint main(){    int N = 9;    int K = 5;     cout << countSequence(N, K);     return 0;}

Java

 // Java program for the above approachimport java.io.*; class GFG {   // Initialize the dp matrixstatic int dp[][] = new int; // Function to find the count of sequences of K// elements with first element as m where every// element is a multiple of the previous onestatic int countSequenceUtil(int k, int m, int n){    // Base case    if (k == 1) {        return 1;    }     // If the value already exists    // in the DP then return it    if (dp[k][m] != -1) {        return dp[k][m];    }     // Variable to store the count    int res = 0;     for (int i = 1; i <= (n / m); i++) {         // Recursive Call        res += countSequenceUtil(k - 1,                                 m * i, n);    }     // Store the calculated    // answer and return it    return dp[k][m] = res;} // Function to find count of sequences of K// elements in the range [1, n] where every// element is a multiple of the previous onestatic int countSequence(int N, int K){       // Initializing all values    // of dp with -1     for(int i=0;i

Python3

 # Python implementation for the above approach # Initialize the dp matrixdp = [[-1 for i in range(1001)] for j in range(1001)] # Function to find the count of sequences of K# elements with first element as m where every# element is a multiple of the previous onedef countSequenceUtil(k, m, n):     # Base case    if (k == 1):        return 1     # If the value already exists    # in the DP then return it    if (dp[k][m] != -1):        return dp[k][m]     # Variable to store the count    res = 0     for i in range(1, (n // m) + 1):         # Recursive Call        res += countSequenceUtil(k - 1,                                 m * i, n)     # Store the calculated    # answer and return it    dp[k][m] = res     return dp[k][m] # Function to find count of sequences of K# elements in the range [1, n] where every# element is a multiple of the previous onedef countSequence(N, K):     # Variable to store    # the total count    ans = 0     # Iterate from 1 to N    for i in range(1, N + 1):        ans += countSequenceUtil(K, i, N)     # Return ans    return ans # Driver CodeN = 9K = 5 print(countSequence(N, K)) # This code is contributed by Saurabh Jaiswal

C#

 // C# program for the above approachusing System;class GFG {     // Initialize the dp matrix    static int[, ] dp = new int[1001, 1001];     // Function to find the count of sequences of K    // elements with first element as m where every    // element is a multiple of the previous one    static int countSequenceUtil(int k, int m, int n)    {               // Base case        if (k == 1) {            return 1;        }         // If the value already exists        // in the DP then return it        if (dp[k, m] != -1) {            return dp[k, m];        }         // Variable to store the count        int res = 0;         for (int i = 1; i <= (n / m); i++) {             // Recursive Call            res += countSequenceUtil(k - 1, m * i, n);        }         // Store the calculated        // answer and return it        return dp[k, m] = res;    }     // Function to find count of sequences of K    // elements in the range [1, n] where every    // element is a multiple of the previous one    static int countSequence(int N, int K)    {         // Initializing all values        // of dp with -1        for (int i = 0; i < dp.GetLength(0); i++) {            for (int j = 0; j < dp.GetLength(1); j++) {                dp[i, j] = -1;            }        }         // Variable to store        // the total count        int ans = 0;         // Iterate from 1 to N        for (int i = 1; i <= N; i++) {            ans += countSequenceUtil(K, i, N);        }         // Return ans        return ans;    }     // Driver Code    public static void Main(string[] args)    {        int N = 9;        int K = 5;         Console.WriteLine(countSequence(N, K));    }} // This code is contributed by ukasp.

Javascript


Output
111

Time Complexity: O(N*K*log N)
Auxiliary Space: O(N*K)

My Personal Notes arrow_drop_up