# Count sequences of length K having each term divisible by its preceding term

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.
Examples:

Input: N = 3, K = 2
Output:
Explaination:
The 5 sequence are [1, 1], [2, 2], [3, 3], [1, 2], [1, 3]
Input: N = 6 K= 4
Output: 39

Approach:
Follow the steps below to solve the problem:

1. Initialize a matrix fct[][] and store the factors of i in the row fct[i], where i lies in the range [1, N].
2. Initialize a matrix dp[][], which stores at dp[i][j], the number of sequences of length i ending with j.
3. 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)).
4. Hence, dp[i][j] should comprise of all possible sequences of (i – 1) length ending with a factor of j.
5. 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.
6. 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:

## C++

 `// C++ Program to implement the` `// above approach`   `#include ` `using` `namespace` `std;`   `#define ll long long`   `// Stores the factors of i-th` `// element in v[i]` `vector vp;`   `// Function to find all the` `// factors of N` `void` `finding_factors(ll ``int` `n)` `{` `    ``ll ``int` `i, a;`   `    ``// Iterate upto sqrt(N)` `    ``for` `(i = 1; i * i <= n; i++) {`   `        ``if` `(n % i == 0) {`   `            ``if` `(i * i == n) {` `                ``vp[n].push_back(i);` `            ``}`   `            ``else` `{` `                ``vp[n].push_back(i);` `                ``vp[n].push_back(n / i);` `            ``}` `        ``}` `    ``}` `}`   `// Function to return the count of` `// sequences of length K having` `// all terms divisible by its` `// preceding term` `ll ``int` `countSeq(ll ``int` `N, ll ``int` `K)` `{` `    ``ll ``int` `i, j, k;`   `    ``ll ``int` `dp = { 0 };`   `    ``for` `(i = 1; i <= N; i++) {`   `        ``// Calculate factors of i` `        ``finding_factors(i);`   `        ``// Initialize dp[i] = 0: No` `        ``// subsequence of length 0 ending` `        ``// with i-th element exists` `        ``dp[i] = 0;`   `        ``// Initialize dp[i] = 1: Only 1` `        ``// subsequence of length 1 ending` `        ``// with i-th element exists` `        ``dp[i] = 1;` `    ``}`   `    ``// Iterate [2, K] to obtain sequences` `    ``// of each length` `    ``for` `(i = 2; i <= K; i++) {`   `        ``for` `(j = 1; j <= N; j++) {`   `            ``// Calculate sum of` `            ``// all dp[i-1][vp[j][k]]`   `            ``ll ``int` `sum = 0;`   `            ``for` `(k = 0; k < vp[j].size(); k++) {`   `                ``// vp[j][k] stores all factors` `                ``// of j` `                ``sum = (sum + dp[i - 1][vp[j][k]]);` `            ``}`   `            ``// Store the sum in A[i][j]` `            ``dp[i][j] = sum;` `        ``}` `    ``}`   `    ``ll ``int` `ans = 0;` `    ``for` `(j = 1; j <= N; j++) {`   `        ``// Sum of all dp[K][j] obtain all` `        ``// K length sequences ending with j` `        ``ans = (ans + dp[K][j]);` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{`   `    ``ll ``int` `N, K;` `    ``N = 3;` `    ``K = 2;`   `    ``cout << countSeq(N, K) << endl;` `    ``return` `0;` `}`

## Java

 `// Java program to implement the` `// above approach` `import` `java.util.*;`   `class` `GFG{`   `// Stores the factors of i-th` `// element in v[i]` `@SuppressWarnings``(``"unchecked"``)` `static` `Vector []vp = ``new` `Vector[``2009``];`   `// Function to find all the` `// factors of N` `static` `void` `finding_factors(``int` `n)` `{` `    ``int` `i, a;`   `    ``// Iterate upto Math.sqrt(N)` `    ``for``(i = ``1``; i * i <= n; i++)` `    ``{` `        ``if` `(n % i == ``0``) ` `        ``{` `            ``if` `(i * i == n) ` `            ``{` `                ``vp[n].add(i);` `            ``}` `            ``else` `            ``{` `                ``vp[n].add(i);` `                ``vp[n].add(n / i);` `            ``}` `        ``}` `    ``}` `}`   `// Function to return the count of` `// sequences of length K having` `// aint terms divisible by its` `// preceding term` `static` `int` `countSeq(``int` `N, ``int` `K)` `{` `    ``int` `i, j, k;`   `    ``int` `dp[][] = ``new` `int``[``109``][``109``];`   `    ``for``(i = ``1``; i <= N; i++) ` `    ``{`   `        ``// Calculate factors of i` `        ``finding_factors(i);`   `        ``// Initialize dp[i] = 0: No` `        ``// subsequence of length 0 ending` `        ``// with i-th element exists` `        ``dp[``0``][i] = ``0``;`   `        ``// Initialize dp[i] = 1: Only 1` `        ``// subsequence of length 1 ending` `        ``// with i-th element exists` `        ``dp[``1``][i] = ``1``;` `    ``}`   `    ``// Iterate [2, K] to obtain sequences` `    ``// of each length` `    ``for``(i = ``2``; i <= K; i++) ` `    ``{` `        ``for``(j = ``1``; j <= N; j++)` `        ``{`   `            ``// Calculate sum of` `            ``// aint dp[i-1][vp[j][k]]` `            ``int` `sum = ``0``;`   `            ``for``(k = ``0``; k < vp[j].size(); k++)` `            ``{`   `                ``// vp[j][k] stores aint factors` `                ``// of j` `                ``sum = (sum + dp[i - ``1``][vp[j].get(k)]);` `            ``}`   `            ``// Store the sum in A[i][j]` `            ``dp[i][j] = sum;` `        ``}` `    ``}`   `    ``int` `ans = ``0``;` `    ``for``(j = ``1``; j <= N; j++) ` `    ``{`   `        ``// Sum of aint dp[K][j] obtain all` `        ``// K length sequences ending with j` `        ``ans = (ans + dp[K][j]);` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `N, K;` `    ``N = ``3``;` `    ``K = ``2``;` `    `  `    ``for``(``int` `i = ``0``; i < vp.length; i++)` `        ``vp[i] = ``new` `Vector();` `        `  `    ``System.out.print(countSeq(N, K) + ``"\n"``);` `}` `}`   `// This code is contributed by gauravrajput1`

## C#

 `// C# program to implement the` `// above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Stores the factors of i-th` `// element in v[i]` `static` `List<``int``> []vp = ``new` `List<``int``>;`   `// Function to find all the` `// factors of N` `static` `void` `finding_factors(``int` `n)` `{` `    ``int` `i ;`   `    ``// Iterate upto Math.Sqrt(N)` `    ``for``(i = 1; i * i <= n; i++)` `    ``{` `        ``if` `(n % i == 0) ` `        ``{` `            ``if` `(i * i == n) ` `            ``{` `                ``vp[n].Add(i);` `            ``}` `            ``else` `            ``{` `                ``vp[n].Add(i);` `                ``vp[n].Add(n / i);` `            ``}` `        ``}` `    ``}` `}`   `// Function to return the count of` `// sequences of length K having` `// aint terms divisible by its` `// preceding term` `static` `int` `countSeq(``int` `N, ``int` `K)` `{` `    ``int` `i, j, k;`   `    ``int` `[,]dp = ``new` `int``[109, 109];`   `    ``for``(i = 1; i <= N; i++) ` `    ``{`   `        ``// Calculate factors of i` `        ``finding_factors(i);`   `        ``// Initialize dp[0,i] = 0: No` `        ``// subsequence of length 0 ending` `        ``// with i-th element exists` `        ``dp[0, i] = 0;`   `        ``// Initialize dp[0,i] = 1: Only 1` `        ``// subsequence of length 1 ending` `        ``// with i-th element exists` `        ``dp[1, i] = 1;` `    ``}`   `    ``// Iterate [2, K] to obtain sequences` `    ``// of each length` `    ``for``(i = 2; i <= K; i++) ` `    ``{` `        ``for``(j = 1; j <= N; j++)` `        ``{`   `            ``// Calculate sum of` `            ``// aint dp[i-1,vp[j,k]]` `            ``int` `sum = 0;`   `            ``for``(k = 0; k < vp[j].Count; k++)` `            ``{`   `                ``// vp[j,k] stores aint factors` `                ``// of j` `                ``sum = (sum + dp[i - 1, vp[j][k]]);` `            ``}`   `            ``// Store the sum in A[i,j]` `            ``dp[i,j] = sum;` `        ``}` `    ``}`   `    ``int` `ans = 0;` `    ``for``(j = 1; j <= N; j++) ` `    ``{`   `        ``// Sum of aint dp[K,j] obtain all` `        ``// K length sequences ending with j` `        ``ans = (ans + dp[K, j]);` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `N, K;` `    ``N = 3;` `    ``K = 2;` `    `  `    ``for``(``int` `i = 0; i < vp.Length; i++)` `        ``vp[i] = ``new` `List<``int``>();` `        `  `    ``Console.Write(countSeq(N, K) + ``"\n"``);` `}` `}`   `// This code is contributed by Rohit_ranjan`

Output:

```5

```

Time Complexity: O (K * N 3/2
Auxiliary Space: O (N 2) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.