Given two integer N and K, the task is to find the number of sequence of length K consisting of values from the range [1, N], such that every (i + 1)th element in the sequence is divisible by its preceeding ith element.
Input: N = 3, K = 2
The 5 sequence are [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]
Input: N = 6 K= 4
Follow the steps below to solve the problem:
- Initialize a matrix fct and store the factors of i in the row fct[i], where i lies in the range [1, N].
- Initialize a matrix dp, which stores at dp[i][j], the number of sequences of length i ending with j.
- If ith index has j, (i – 1)th index should consist of factor(j). Similarly, (i – 2)th index should consist of a factor(factor(j)).
- Hence, dp[i][j] should comprise of all possible sequences of (i – 1) length ending with a factor of j.
- Hence, dp[i][j] is equal to the sum of all possible dp[i – 1][fct[j][k]], where dp[i – 1][fct[j][k]] denotes the the count of total sequences of length i – 1 ending with kth factor of j.
- Finaly find the sum of all dp[K][j] from 1<=j<=N and return the sum.
Below is the implementation of the above approach:
Time Complexity: O (K * N 3/2)
Auxiliary Space: O (N 2)
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Count array elements exceeding sum of preceding K elements
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Count all sub-sequences having product <= K - Recursive approach
- Count of subarrays having sum equal to its length
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Count even length binary sequences with same sum of first and second half bits
- Count of odd length contiguous Palindromic sequences in a Matrix
- Count of node sequences of length K consisting of at least one black edge
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of binary strings of length N having equal count of 0's and 1's
- Print all sequences of given length
- Print all increasing sequences of length k from first n natural numbers
- Find all even length binary sequences with same sum of first and second half bits
- Sequences of given length where every element is more than or equal to twice of previous
- Number of N length sequences whose product is M
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.