Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AICount of N-size Arrays that can be formed starting with K such...

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

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:

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 <bits/stdc++.h>
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 total 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 total 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 total 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 total 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




<script>
       // JavaScript Program to implement
       // the above approach
       let MOD = 1000000007;
 
       // Find the value of x raised to the
       // yth power modulo MOD
       function modPow(x, y)
       {
        
           // Stores the value of x^y
           let 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
       function modInverse(x) {
           return modPow(x, MOD - 2);
       }
 
       // Modular division x / y, find
       // modular multiplicative inverse
       // of y and multiply by x
       function modDivision(p, q) {
           return (p * modInverse(q)) % MOD;
       }
 
       // Function to find Binomial Coefficient
       // C(n, k) in O(k) time
       function C(n, k)
       {
        
           // Base Case
           if (k > n) {
               return 0;
           }
           let p = 1, q = 1;
 
           for (let 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
       function countArrays(N, K)
       {
        
           // Stores the resultant count of arrays
           let res = 1;
 
           // Find the factorization of K
           for (let p = 2; p <= K / p; p++) {
 
               let 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 total count
           return res;
       }
 
       // Driver Code
       let N = 3, K = 5;
       document.write(countArrays(N, K));
 
    // This code is contributed by Potta Lokesh
   </script>


Output: 

3

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
25 Jan, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments