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:
- {5, 5, 5}
- {5, 5, 1}
- {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++
#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
long modPow( long x, long y)
{
long r = 1, a = x;
while (y > 0) {
if ((y & 1) == 1) {
r = (r * a) % MOD;
}
a = (a * a) % MOD;
y /= 2;
}
return r;
}
long modInverse( long x)
{
return modPow(x, MOD - 2);
}
long modDivision( long p, long q)
{
return (p * modInverse(q)) % MOD;
}
long C( long n, int k)
{
if (k > n) {
return 0;
}
long p = 1, q = 1;
for ( int i = 1; i <= k; i++) {
q = (q * i) % MOD;
p = (p * (n - i + 1)) % MOD;
}
return modDivision(p, q);
}
int countArrays( int N, int K)
{
long res = 1;
for ( int p = 2; p <= K / p; p++) {
int c = 0;
while (K % p == 0) {
K /= p;
c++;
}
res = (res * C(N - 1 + c, c))
% MOD;
}
if (N > 1) {
res = (res * N) % MOD;
}
return res;
}
int main()
{
int N = 3, K = 5;
cout << countArrays(N, K);
return 0;
}
|
Java
class GFG {
public static int MOD = 1000000007 ;
public static long modPow( long x, long y)
{
long r = 1 , a = x;
while (y > 0 ) {
if ((y & 1 ) == 1 ) {
r = (r * a) % MOD;
}
a = (a * a) % MOD;
y /= 2 ;
}
return r;
}
public static long modInverse( long x) {
return modPow(x, MOD - 2 );
}
public static long modDivision( long p, long q) {
return (p * modInverse(q)) % MOD;
}
public static long C( long n, int k) {
if (k > n) {
return 0 ;
}
long p = 1 , q = 1 ;
for ( int i = 1 ; i <= k; i++) {
q = (q * i) % MOD;
p = (p * (n - i + 1 )) % MOD;
}
return modDivision(p, q);
}
public static long countArrays( int N, int K) {
long res = 1 ;
for ( int p = 2 ; p <= K / p; p++) {
int c = 0 ;
while (K % p == 0 ) {
K /= p;
c++;
}
res = (res * C(N - 1 + c, c)) % MOD;
}
if (N > 1 ) {
res = (res * N) % MOD;
}
return res;
}
public static void main(String args[]) {
int N = 3 , K = 5 ;
System.out.println(countArrays(N, K));
}
}
|
Python3
MOD = 1000000007
from math import sqrt
def modPow(x, y):
r = 1
a = x
while (y > 0 ):
if ((y & 1 ) = = 1 ):
r = (r * a) % MOD
a = (a * a) % MOD
y / = 2
return r
def modInverse(x):
return modPow(x, MOD - 2 )
def modDivision(p, q):
return (p * modInverse(q)) % MOD
def C(n, k):
if (k > n):
return 0
p = 1
q = 1
for i in range ( 1 ,k + 1 , 1 ):
q = (q * i) % MOD
p = (p * (n - i + 1 )) % MOD
return modDivision(p, q)
def countArrays(N, K):
res = 1
for p in range ( 2 , int (sqrt(K)), 1 ):
c = 0
while (K % p = = 0 ):
K / = p
c + = 1
res = (res * C(N - 1 + c, c)) % MOD
if (N > 1 ):
res = (res * N) % MOD
return res
if __name__ = = '__main__' :
N = 3
K = 5
print (countArrays(N, K))
|
C#
using System;
public class GFG
{
public static int MOD = 1000000007;
public static long modPow( long x, long y)
{
long r = 1, a = x;
while (y > 0) {
if ((y & 1) == 1) {
r = (r * a) % MOD;
}
a = (a * a) % MOD;
y /= 2;
}
return r;
}
public static long modInverse( long x) {
return modPow(x, MOD - 2);
}
public static long modDivision( long p, long q) {
return (p * modInverse(q)) % MOD;
}
public static long C( long n, int k) {
if (k > n) {
return 0;
}
long p = 1, q = 1;
for ( int i = 1; i <= k; i++) {
q = (q * i) % MOD;
p = (p * (n - i + 1)) % MOD;
}
return modDivision(p, q);
}
public static long countArrays( int N, int K) {
long res = 1;
for ( int p = 2; p <= K / p; p++) {
int c = 0;
while (K % p == 0) {
K /= p;
c++;
}
res = (res * C(N - 1 + c, c)) % MOD;
}
if (N > 1) {
res = (res * N) % MOD;
}
return res;
}
static public void Main (){
int N = 3, K = 5;
Console.WriteLine(countArrays(N, K));
}
}
|
Javascript
<script>
let MOD = 1000000007;
function modPow(x, y)
{
let r = 1, a = x;
while (y > 0) {
if ((y & 1) == 1) {
r = (r * a) % MOD;
}
a = (a * a) % MOD;
y /= 2;
}
return r;
}
function modInverse(x) {
return modPow(x, MOD - 2);
}
function modDivision(p, q) {
return (p * modInverse(q)) % MOD;
}
function C(n, k)
{
if (k > n) {
return 0;
}
let p = 1, q = 1;
for (let i = 1; i <= k; i++) {
q = (q * i) % MOD;
p = (p * (n - i + 1)) % MOD;
}
return modDivision(p, q);
}
function countArrays(N, K)
{
let res = 1;
for (let p = 2; p <= K / p; p++) {
let c = 0;
while (K % p == 0) {
K /= p;
c++;
}
res = (res * C(N - 1 + c, c))
% MOD;
}
if (N > 1) {
res = (res * N) % MOD;
}
return res;
}
let N = 3, K = 5;
document.write(countArrays(N, K));
</script>
|
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 geeks!