Count different numbers possible using all the digits their frequency times
Last Updated :
24 Apr, 2023
Given an array arr[] which contains the frequency of the digits (0-9), the task is to find the count of numbers possible using each digit its frequency times. That is, the final numbers generated should contain all of the digits their frequency times. Since, the count can be very large return the answer modulo 10^9+7. Prerequisites: Factorial of a number, Modular multiplicative inverse Examples:
Input : arr[] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0}
Output : 2
Frequency of 1 and 2 is 1 and all the rest is 0.
Therefore, 2 possible numbers are 12 and 21.
Input : arr[] = {0, 5, 2, 0, 0, 0, 4, 0, 1, 1}
Output : 1081080
Approach: The problem can be easily solved using Permutation and Combinations. The ith index of array arr[] gives the frequency of ith digit. The problem can be thought of as finding the total permutation of X objects where X0 objects are of one type, X1 objects are of another type and so on. Then the possible count of numbers is given by:
Total Count = X! / (X0! * X1! *…..* X9!)
where X = Sum of frequencies of all the digits and Xi = the frequency of ith digit. Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100000
#define MOD 1000000007
ll fact[MAXN];
void factorial()
{
fact[0] = 1;
for ( int i = 1; i < MAXN; i++)
fact[i] = (fact[i - 1] * i) % MOD;
}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll modInverse(ll x, ll p)
{
return power(x, p - 2, p);
}
ll countDifferentNumbers(ll arr[], ll P)
{
factorial();
ll res = 0, X = 0;
for ( int i = 0; i < 10; i++)
X += arr[i];
res = fact[X];
for ( int i = 0; i < 10; i++) {
if (arr[i] > 1)
res = (res * modInverse(fact[arr[i]], P)) % P;
}
return res;
}
int main()
{
ll arr[] = { 1, 0, 2, 0, 0, 7, 4, 0, 0, 3 };
cout << countDifferentNumbers(arr, MOD);
return 0;
}
|
Java
class GFG
{
static int MAXN = 100000 ;
static int MOD = 1000000007 ;
static long fact[] = new long [MAXN];
static void factorial()
{
fact[ 0 ] = 1 ;
for ( int i = 1 ; i < MAXN; i++)
fact[i] = (fact[i - 1 ] * i) % MOD;
}
static long power( long x, long y, long p)
{
long res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static long modInverse( long x, long p)
{
return power(x, p - 2 , p);
}
static long countDifferentNumbers( long arr[], long P)
{
factorial();
long res = 0 , X = 0 ;
for ( int i = 0 ; i < 10 ; i++)
X += arr[i];
res = fact[( int )X];
for ( int i = 0 ; i < 10 ; i++)
{
if (arr[i] > 1 )
res = (res * modInverse(fact[( int )arr[i]], P)) % P;
}
return res;
}
public static void main(String[] args)
{
long arr[] = { 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 };
System.out.println(countDifferentNumbers(arr, MOD));
}
}
|
Python3
MAXN = 100000
MOD = 1000000007
fact = [ 0 ] * MAXN;
def factorial() :
fact[ 0 ] = 1
for i in range ( 1 , MAXN) :
fact[i] = (fact[i - 1 ] * i) % MOD
def power(x, y, p) :
res = 1
x = x % p
while (y > 0 ) :
if (y & 1 ) :
res = (res * x) % p
y = y >> 1 ;
x = (x * x) % p
return res
def modInverse(x, p) :
return power(x, p - 2 , p)
def countDifferentNumbers(arr, P) :
factorial();
res = 0 ; X = 0 ;
for i in range ( 10 ) :
X + = arr[i]
res = fact[X]
for i in range ( 10 ) :
if (arr[i] > 1 ) :
res = (res * modInverse(fact[arr[i]], P)) % P;
return res;
if __name__ = = "__main__" :
arr = [ 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 ]
print (countDifferentNumbers(arr, MOD))
|
C#
using System;
class GFG
{
static int MAXN = 100000;
static int MOD = 1000000007;
static long []fact = new long [MAXN];
static void factorial()
{
fact[0] = 1;
for ( int i = 1; i < MAXN; i++)
fact[i] = (fact[i - 1] * i) % MOD;
}
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2== 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse( long x, long p)
{
return power(x, p - 2, p);
}
static long countDifferentNumbers( long []arr, long P)
{
factorial();
long res = 0, X = 0;
for ( int i = 0; i < 10; i++)
X += arr[i];
res = fact[( int )X];
for ( int i = 0; i < 10; i++)
{
if (arr[i] > 1)
res = (res * modInverse(fact[( int )arr[i]], P)) % P;
}
return res;
}
public static void Main(String[] args)
{
long []arr = { 1, 0, 2, 0, 0, 7, 4, 0, 0, 3 };
Console.WriteLine(countDifferentNumbers(arr, MOD));
}
}
|
Javascript
let MAXN = 100000n
let MOD = 1000000007n
let fact = new Array(MAXN).fill(0n);
function factorial()
{
fact[0] = 1n
for ( var i = 1n; i < MAXN; i++)
fact[i] = (fact[i - 1n] * i) % MOD
}
function power(x, y, p)
{
let res = 1n
x = x % p
while (y > 0n)
{
if (y & 1n)
res = (res * x) % p
y = y >> 1n;
x = (x * x) % p
}
return res
}
function modInverse(x, p)
{
return power(x, p - 2n, p)
}
function countDifferentNumbers(arr, P)
{
factorial();
let res = 0n;
let X = 0n;
for ( var i = 0n; i < 10n; i++)
X += arr[i]
res = fact[X]
for ( var i = 0n; i < 10n; i++)
if (arr[i] > 1n)
res = (res * modInverse(fact[arr[i]], P)) % P;
return res;
}
let arr = [1n, 0n, 2n, 0n, 0n, 7n, 4n, 0n, 0n, 3n ]
console.log(countDifferentNumbers(arr, MOD))
|
Time Complexity: O(MAXN)
Auxiliary Space: O(MAXN)
Share your thoughts in the comments
Please Login to comment...