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 preceding ith element.
Examples:
Input: N = 3, K = 2
Output: 5
Explanation:
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:
- 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 count of total sequences of length i – 1 ending with kth factor of j.
- Finally, 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++14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll int > vp[2009];
void finding_factors(ll int n)
{
ll int i, a;
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);
}
}
}
}
ll int countSeq(ll int N, ll int K)
{
ll int i, j, k;
ll int dp[109][109] = { 0 };
for (i = 1; i <= N; i++) {
finding_factors(i);
dp[0][i] = 0;
dp[1][i] = 1;
}
for (i = 2; i <= K; i++) {
for (j = 1; j <= N; j++) {
ll int sum = 0;
for (k = 0; k < vp[j].size(); k++) {
sum = (sum + dp[i - 1][vp[j][k]]);
}
dp[i][j] = sum;
}
}
ll int ans = 0;
for (j = 1; j <= N; j++) {
ans = (ans + dp[K][j]);
}
return ans;
}
int main()
{
ll int N, K;
N = 3;
K = 2;
cout << countSeq(N, K) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
@SuppressWarnings ( "unchecked" )
static Vector<Integer> []vp = new Vector[ 2009 ];
static void finding_factors( int n)
{
int i, a;
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);
}
}
}
}
static int countSeq( int N, int K)
{
int i, j, k;
int dp[][] = new int [ 109 ][ 109 ];
for (i = 1 ; i <= N; i++)
{
finding_factors(i);
dp[ 0 ][i] = 0 ;
dp[ 1 ][i] = 1 ;
}
for (i = 2 ; i <= K; i++)
{
for (j = 1 ; j <= N; j++)
{
int sum = 0 ;
for (k = 0 ; k < vp[j].size(); k++)
{
sum = (sum + dp[i - 1 ][vp[j].get(k)]);
}
dp[i][j] = sum;
}
}
int ans = 0 ;
for (j = 1 ; j <= N; j++)
{
ans = (ans + dp[K][j]);
}
return ans;
}
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<Integer>();
System.out.print(countSeq(N, K) + "\n" );
}
}
|
Python3
vp = [[] for i in range ( 2009 )]
def finding_factors(n):
i = 1
a = 0
global vp
while (i * i < = n):
if (n % i = = 0 ):
if (i * i = = n):
vp[n].append(i)
else :
vp[n].append(i)
vp[n].append( int (n / i))
i + = 1
def countSeq(N, K):
i = 0
j = 0
k = 0
dp = [[ 0 for i in range ( 109 )]
for j in range ( 109 )]
for i in range ( 1 , N + 1 ):
finding_factors(i)
dp[ 0 ][i] = 0
dp[ 1 ][i] = 1
for i in range ( 2 , K + 1 ):
for j in range ( 1 , N + 1 ):
Sum = 0
for k in range ( len (vp[j])):
Sum + = dp[i - 1 ][vp[j][k]]
dp[i][j] = Sum
ans = 0
for j in range ( 1 , N + 1 ):
ans + = dp[K][j]
return ans
N = 3
K = 2
print (countSeq(N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > []vp = new List< int >[2009];
static void finding_factors( int n)
{
int i ;
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);
}
}
}
}
static int countSeq( int N, int K)
{
int i, j, k;
int [,]dp = new int [109, 109];
for (i = 1; i <= N; i++)
{
finding_factors(i);
dp[0, i] = 0;
dp[1, i] = 1;
}
for (i = 2; i <= K; i++)
{
for (j = 1; j <= N; j++)
{
int sum = 0;
for (k = 0; k < vp[j].Count; k++)
{
sum = (sum + dp[i - 1, vp[j][k]]);
}
dp[i,j] = sum;
}
}
int ans = 0;
for (j = 1; j <= N; j++)
{
ans = (ans + dp[K, j]);
}
return ans;
}
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" );
}
}
|
Javascript
<script>
let vp = new Array(2009);
function finding_factors(n)
{
let i, a;
for (i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
if (i * i == n)
{
vp[n].push(i);
}
else
{
vp[n].push(i);
vp[n].push(n / i);
}
}
}
}
function countSeq(N, K)
{
let i, j, k;
let dp = new Array(109);
for (let i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (i = 1; i <= N; i++)
{
finding_factors(i);
dp[0][i] = 0;
dp[1][i] = 1;
}
for (i = 2; i <= K; i++)
{
for (j = 1; j <= N; j++)
{
let sum = 0;
for (k = 0; k < vp[j].length; k++)
{
sum = (sum + dp[i - 1][vp[j][k]]);
}
dp[i][j] = sum;
}
}
let ans = 0;
for (j = 1; j <= N; j++)
{
ans = (ans + dp[K][j]);
}
return ans;
}
let N, K;
N = 3;
K = 2;
for (let i = 0; i < vp.length; i++)
vp[i] = [];
document.write(countSeq(N, K) + "\n" );
</script>
|
Time Complexity: O (K * N 3/2)
Auxiliary Space: O (N 2)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...