# Count of N-size Arrays that can be formed starting with K such that every element is divisible by next

• Last Updated : 28 Sep, 2021

Given two integers N and K, the task is to find the number of different arrays of size N that can be formed having the first element as K such that every element except last, is divisible by the next element in the array. Since the count can be very large, so print the modulo 109 + 7.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 3, K = 5
Output: 3
Explanation:
There are 3 possible valid array starting with value 5 satisfying the given criteria:

1. {5, 5, 5}
2. {5, 5, 1}
3. {5, 1, 1}.

Therefore the total count is 3.

Input: N = 3, K = 6
Output: 9

Approach: The given problem can be solved by using the Number Theory and Combinatorics. The first element of the array is K, then the next array element is one of the factors of the K. Follow the steps below to solve the given problem:

• Initialize a variable, say res as 1 that stores the resultant count of arrays formed.
• Find all the powers of prime factors of the number K and for each prime factor P perform the following steps:
• Find the number of times P occurs in the value K. Let that count be count.
• The number of possible ways to keep one of the factors of P is given by (N – count + 1)Ccount.
• Multiply the value (N – count + 1)Ccount with the value res.
• After completing the above steps, print the value of res as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;``int` `MOD = 1000000007;` `// Find the value of x raised to the``// yth power modulo MOD``long` `modPow(``long` `x, ``long` `y)``{``    ``// Stores the value of x^y``    ``long` `r = 1, a = x;` `    ``// Iterate until y is positive``    ``while` `(y > 0) {``        ``if` `((y & 1) == 1) {``            ``r = (r * a) % MOD;``        ``}``        ``a = (a * a) % MOD;``        ``y /= 2;``    ``}` `    ``return` `r;``}``// Function to perform the Modular``// Multiplicative Inverse using the``// Fermat's little theorem``long` `modInverse(``long` `x)``{``    ``return` `modPow(x, MOD - 2);``}` `// Modular division x / y, find``// modular multiplicative inverse``// of y and multiply by x``long` `modDivision(``long` `p, ``long` `q)``{``    ``return` `(p * modInverse(q)) % MOD;``}` `// Function to find Binomial Coefficient``// C(n, k) in O(k) time``long` `C(``long` `n, ``int` `k)``{``    ``// Base Case``    ``if` `(k > n) {``        ``return` `0;``    ``}``    ``long` `p = 1, q = 1;` `    ``for` `(``int` `i = 1; i <= k; i++) {` `        ``// Update the value of p and q``        ``q = (q * i) % MOD;``        ``p = (p * (n - i + 1)) % MOD;``    ``}` `    ``return` `modDivision(p, q);``}` `// Function to find the count of arrays``// having K as the first element satisfying``// the given criteria``int` `countArrays(``int` `N, ``int` `K)``{``    ``// Stores the resultant count of arrays``    ``long` `res = 1;` `    ``// Find the factorization of K``    ``for` `(``int` `p = 2; p <= K / p; p++) {` `        ``int` `c = 0;` `        ``// Stores the count of the exponent``        ``// of the currentprime factor``        ``while` `(K % p == 0) {``            ``K /= p;``            ``c++;``        ``}``        ``res = (res * C(N - 1 + c, c))``              ``% MOD;``    ``}``    ``if` `(N > 1) {` `        ``// N is one last prime factor,``        ``// for c = 1 -> C(N-1+1, 1) = N``        ``res = (res * N) % MOD;``    ``}` `    ``// Return the totol count``    ``return` `res;``}` `// Driver Code``int` `main()``{``    ``int` `N = 3, K = 5;``    ``cout << countArrays(N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG {` `    ``public` `static` `int` `MOD = ``1000000007``;` `    ``// Find the value of x raised to the``    ``// yth power modulo MOD``    ``public` `static` `long` `modPow(``long` `x, ``long` `y)``    ``{``      ` `        ``// Stores the value of x^y``        ``long` `r = ``1``, a = x;` `        ``// Iterate until y is positive``        ``while` `(y > ``0``) {``            ``if` `((y & ``1``) == ``1``) {``                ``r = (r * a) % MOD;``            ``}``            ``a = (a * a) % MOD;``            ``y /= ``2``;``        ``}` `        ``return` `r;``    ``}` `    ``// Function to perform the Modular``    ``// Multiplicative Inverse using the``    ``// Fermat's little theorem``    ``public` `static` `long` `modInverse(``long` `x) {``        ``return` `modPow(x, MOD - ``2``);``    ``}` `    ``// Modular division x / y, find``    ``// modular multiplicative inverse``    ``// of y and multiply by x``    ``public` `static` `long` `modDivision(``long` `p, ``long` `q) {``        ``return` `(p * modInverse(q)) % MOD;``    ``}` `    ``// Function to find Binomial Coefficient``    ``// C(n, k) in O(k) time``    ``public` `static` `long` `C(``long` `n, ``int` `k) {``        ``// Base Case``        ``if` `(k > n) {``            ``return` `0``;``        ``}``        ``long` `p = ``1``, q = ``1``;` `        ``for` `(``int` `i = ``1``; i <= k; i++) {` `            ``// Update the value of p and q``            ``q = (q * i) % MOD;``            ``p = (p * (n - i + ``1``)) % MOD;``        ``}` `        ``return` `modDivision(p, q);``    ``}` `    ``// Function to find the count of arrays``    ``// having K as the first element satisfying``    ``// the given criteria``    ``public` `static` `long` `countArrays(``int` `N, ``int` `K) {``        ``// Stores the resultant count of arrays``        ``long` `res = ``1``;` `        ``// Find the factorization of K``        ``for` `(``int` `p = ``2``; p <= K / p; p++) {` `            ``int` `c = ``0``;` `            ``// Stores the count of the exponent``            ``// of the currentprime factor``            ``while` `(K % p == ``0``) {``                ``K /= p;``                ``c++;``            ``}``            ``res = (res * C(N - ``1` `+ c, c)) % MOD;``        ``}``        ``if` `(N > ``1``) {` `            ``// N is one last prime factor,``            ``// for c = 1 -> C(N-1+1, 1) = N``            ``res = (res * N) % MOD;``        ``}` `        ``// Return the totol count``        ``return` `res;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {``        ``int` `N = ``3``, K = ``5``;``        ``System.out.println(countArrays(N, K));` `    ``}``}` `// This code is contributed by saurabh_jaiswal.`

## Python3

 `# python 3 program for the above approach``MOD ``=` `1000000007` `from` `math ``import` `sqrt` `# Find the value of x raised to the``# yth power modulo MOD``def` `modPow(x, y):``  ` `    ``# Stores the value of x^y``    ``r ``=` `1``    ``a ``=` `x` `    ``# Iterate until y is positive``    ``while``(y > ``0``):``        ``if` `((y & ``1``) ``=``=` `1``):``            ``r ``=` `(r ``*` `a) ``%` `MOD``        ``a ``=` `(a ``*` `a) ``%` `MOD``        ``y ``/``=` `2` `    ``return` `r``# Function to perform the Modular``# Multiplicative Inverse using the``# Fermat's little theorem``def` `modInverse(x):``    ``return` `modPow(x, MOD ``-` `2``)` `# Modular division x / y, find``# modular multiplicative inverse``# of y and multiply by x``def` `modDivision(p, q):``    ``return` `(p ``*` `modInverse(q)) ``%` `MOD` `# Function to find Binomial Coefficient``# C(n, k) in O(k) time``def` `C(n, k):``    ``# Base Case``    ``if` `(k > n):``        ``return` `0` `    ``p ``=` `1``    ``q ``=` `1` `    ``for` `i ``in` `range``(``1``,k``+``1``,``1``):` `        ``# Update the value of p and q``        ``q ``=` `(q ``*` `i) ``%` `MOD``        ``p ``=` `(p ``*` `(n ``-` `i ``+` `1``)) ``%` `MOD` `    ``return` `modDivision(p, q)` `# Function to find the count of arrays``# having K as the first element satisfying``# the given criteria``def` `countArrays(N, K):``    ``# Stores the resultant count of arrays``    ``res ``=` `1` `    ``# Find the factorization of K``    ``for` `p ``in` `range``(``2``,``int``(sqrt(K)),``1``):``        ``c ``=` `0` `        ``# Stores the count of the exponent``        ``# of the currentprime factor``        ``while` `(K ``%` `p ``=``=` `0``):``            ``K ``/``=` `p``            ``c ``+``=` `1``        ``res ``=` `(res ``*` `C(N ``-` `1` `+` `c, c)) ``%` `MOD``    ``if` `(N > ``1``):` `        ``# N is one last prime factor,``        ``# for c = 1 -> C(N-1+1, 1) = N``        ``res ``=` `(res ``*` `N) ``%` `MOD` `    ``# Return the totol count``    ``return` `res` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``N ``=` `3``    ``K ``=` `5``    ``print``(countArrays(N, K))``    ` `    ``# This code is contributed by SURENDRA_GANGWAR.`

## C#

 `// C# program for the above approach``using` `System;``public` `class` `GFG``{   ``      ``public` `static` `int` `MOD = 1000000007;` `    ``// Find the value of x raised to the``    ``// yth power modulo MOD``    ``public` `static` `long` `modPow(``long` `x, ``long` `y)``    ``{``      ` `        ``// Stores the value of x^y``        ``long` `r = 1, a = x;` `        ``// Iterate until y is positive``        ``while` `(y > 0) {``            ``if` `((y & 1) == 1) {``                ``r = (r * a) % MOD;``            ``}``            ``a = (a * a) % MOD;``            ``y /= 2;``        ``}` `        ``return` `r;``    ``}` `    ``// Function to perform the Modular``    ``// Multiplicative Inverse using the``    ``// Fermat's little theorem``    ``public` `static` `long` `modInverse(``long` `x) {``        ``return` `modPow(x, MOD - 2);``    ``}` `    ``// Modular division x / y, find``    ``// modular multiplicative inverse``    ``// of y and multiply by x``    ``public` `static` `long` `modDivision(``long` `p, ``long` `q) {``        ``return` `(p * modInverse(q)) % MOD;``    ``}` `    ``// Function to find Binomial Coefficient``    ``// C(n, k) in O(k) time``    ``public` `static` `long` `C(``long` `n, ``int` `k) {``        ``// Base Case``        ``if` `(k > n) {``            ``return` `0;``        ``}``        ``long` `p = 1, q = 1;` `        ``for` `(``int` `i = 1; i <= k; i++) {` `            ``// Update the value of p and q``            ``q = (q * i) % MOD;``            ``p = (p * (n - i + 1)) % MOD;``        ``}` `        ``return` `modDivision(p, q);``    ``}` `    ``// Function to find the count of arrays``    ``// having K as the first element satisfying``    ``// the given criteria``    ``public` `static` `long` `countArrays(``int` `N, ``int` `K) {``        ``// Stores the resultant count of arrays``        ``long` `res = 1;` `        ``// Find the factorization of K``        ``for` `(``int` `p = 2; p <= K / p; p++) {` `            ``int` `c = 0;` `            ``// Stores the count of the exponent``            ``// of the currentprime factor``            ``while` `(K % p == 0) {``                ``K /= p;``                ``c++;``            ``}``            ``res = (res * C(N - 1 + c, c)) % MOD;``        ``}``        ``if` `(N > 1) {` `            ``// N is one last prime factor,``            ``// for c = 1 -> C(N-1+1, 1) = N``            ``res = (res * N) % MOD;``        ``}` `        ``// Return the totol count``        ``return` `res;``    ``}` `    ``// Driver Code``    ``static` `public` `void` `Main (){` `        ``int` `N = 3, K = 5;``        ``Console.WriteLine(countArrays(N, K));``    ``}``}` `// This code is contributed by Dharanendra L V.`

## Javascript

 ``

Output:
`3`

Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up