Number of N length sequences whose product is M
Given two integers N and M, the task is to find the count of possible sequences a1, a2, … of length N such that the product of all the elements of the sequence is M.
Examples:
Input: N = 2, M = 6
Output: 4
Possible sequences are {1, 6}, {2, 3}, {3, 2} and {6, 1}
Input: N = 3, M = 24
Output: 30
Approach:
- Consider the prime factorisation of M as p1k1p2k2 …pzkz.
- For each prime factor, its exponent has to be distributed among N different groups because on multiplying those N terms, the exponents of the prime factors will add. This can be done using the formula explained here.
- For each prime factor, the number of ways of distributing its exponents in N sequences would be equal to
N + Ki -1CN-1 for every 1 ? i ? z.
- Using Fundamental Principle of Multiplication, multiply the results of all the prime factors to get the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ncr( int n, int r)
{
int res = 1;
for ( int i = 1; i <= r; i += 1) {
res *= (n - r + i);
res /= i;
}
return res;
}
int NoofSequences( int N, int M)
{
unordered_map< int , int > prime;
for ( int i = 2; i <= sqrt (M); i += 1) {
while (M % i == 0) {
prime[i] += 1;
M /= i;
}
}
if (M > 1) {
prime[M] += 1;
}
int ans = 1;
for ( auto it : prime) {
ans *= (ncr(N + it.second - 1, N - 1));
}
return ans;
}
int main()
{
int N = 2, M = 6;
cout << NoofSequences(N, M);
return 0;
}
|
Java
import java.util.HashMap;
class geeks
{
public static int nCr( int n, int r)
{
int res = 1 ;
for ( int i = 1 ; i <= r; i++)
{
res *= (n - r + i);
res /= i;
}
return res;
}
public static int NoofSequences( int N, int M)
{
HashMap<Integer, Integer> prime = new HashMap<>();
for ( int i = 2 ; i <= Math.sqrt(M); i++)
{
while (M % i == 0 )
{
if (prime.get(i) == null )
prime.put(i, 1 );
else
{
int x = prime.get(i);
prime.put(i, ++x);
}
M /= i;
}
}
if (M > 1 )
{
if (prime.get(M) != null )
{
int x = prime.get(M);
prime.put(M, ++x);
}
else
prime.put(M, 1 );
}
int ans = 1 ;
for (HashMap.Entry<Integer, Integer> entry : prime.entrySet())
{
ans *= (nCr(N + entry.getValue() - 1 , N - 1 ));
}
return ans;
}
public static void main(String[] args)
{
int N = 2 , M = 6 ;
System.out.println(NoofSequences(N, M));
}
}
|
Python3
def ncr(n, r):
res = 1
for i in range ( 1 ,r + 1 ):
res * = (n - r + i)
res / / = i
return res
def NoofSequences(N, M):
prime = {}
for i in range ( 2 , int (M * * (. 5 )) + 1 ):
while (M % i = = 0 ):
prime[i] = prime.get(i, 0 ) + 1
M / / = i
if (M > 1 ):
prime[M] = prime.get(M, 0 ) + 1
ans = 1
for it in prime:
ans * = (ncr(N + prime[it] - 1 , N - 1 ))
return ans
N = 2
M = 6
print (NoofSequences(N, M))
|
C#
using System;
using System.Collections.Generic;
public class geeks
{
public static int nCr( int n, int r)
{
int res = 1;
for ( int i = 1; i <= r; i++)
{
res *= (n - r + i);
res /= i;
}
return res;
}
public static int NoofSequences( int N, int M)
{
Dictionary< int , int >prime = new Dictionary< int , int >();
for ( int i = 2; i <= Math.Sqrt(M); i++)
{
while (M % i == 0)
{
if (!prime.ContainsKey(i))
prime.Add(i, 1);
else
{
int x = prime[i];
prime.Remove(i);
prime.Add(i, ++x);
}
M /= i;
}
}
if (M > 1)
{
if (prime.ContainsKey(M))
{
int x = prime[M];
prime.Remove(M);
prime.Add(M, ++x);
}
else
prime.Add(M, 1);
}
int ans = 1;
foreach (KeyValuePair< int , int > entry in prime)
{
ans *= (nCr(N + entry.Value - 1, N - 1));
}
return ans;
}
public static void Main(String[] args)
{
int N = 2, M = 6;
Console.WriteLine(NoofSequences(N, M));
}
}
|
Javascript
<script>
function nCr(n,r)
{
let res = 1;
for (let i = 1; i <= r; i++)
{
res *= (n - r + i);
res =Math.floor(res/ i);
}
return res;
}
function NoofSequences(N,M)
{
let prime = new Map();
for (let i = 2; i <= Math.sqrt(M); i++)
{
while (M % i == 0)
{
if (prime.get(i) == null )
prime.set(i, 1);
else
{
let x = prime.get(i);
prime.set(i, ++x);
}
M = Math.floor(M/i);
}
}
if (M > 1)
{
if (prime.get(M) != null )
{
let x = prime.get(M);
prime.set(M, ++x);
}
else
prime.set(M, 1);
}
let ans = 1;
for (let [key, value] of prime.entries())
{
ans *= (nCr(N + value - 1, N - 1));
}
return ans;
}
let N = 2, M = 6;
document.write(NoofSequences(N, M));
</script>
|
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...