Count Numbers formed by given two Digit with Sum having given Digits


The problem statement includes counting the numbers formed by the given two digits, x and y of size N with sum having given digits only i.e. x and y.

We need to count the distinct numbers which can be formed by the digits, x and y which will be the user input of size N where N ranges from 1 to 10^6. The N will also be provided in the input. The numbers formed using the digits, x and y of size N must be such that the sum of digits of the numbers formed should have only digits x and y.

Since the value of N can be very large, the count of numbers could be very large so we need to return the answer in modulo 1000000007 (i.e. 1e9+7).

The problem can be better understood with the below illustration.

Input

x=4 , y=2  , N=2

Output

1

Explanation − Here the input given is x=4,y=2 and N=2 which means we need to find the count of numbers containing 2 digits. The digits should only be 4 and 2 and the number should be such that the sum of digits of the number should contain only digits 4 and 2.

The only possible number which satisfies the conditions is 22. It contains 2 digits and is formed with given digits and the sum of the digits of the number is equal to 4 i.e. the sum only contains given digits. So, 1 is the required answer for this input.

Input

x=1 , y=3 , N=5

Output

15

Explanation − We need to find the count of numbers formed using the digits 1 and 3 of size 5 such that their sum of digits should only contain the given digits. Such numbers can be,

33331, 33313, 33133, 31333, 13333, 33311, 33113, 31133, 11333, 13331, 13313, 13133, 31313, 31331, 33131.

Let’s understand the algorithm to find the count of numbers formed using the given digits such that their sum of digits contains only the given digits.

Algorithm

The value of N given will be large so it is not possible to check every possible number if the sum of digits of those numbers contains only given digits or not. As we just need to count the numbers formed by digits x and y of size N, we can simply check for the number of times a digit repeats in the number formed.

Let’s say x appears i times in the number, so y will appear (N−i) in the number since the number is only formed using x and y digits and the number of digits in the number is N. So the sum of the number will be (x*i + (N−i)*y).

If the sum contains only x and y then we can find the count of numbers formed using i times x and (N−i) times y in the number using combinatorics.

The count of numbers can be found out using the below formula:

$\mathrm{^NC_{i}=\frac{N!}{(N−i)!*i!}or\:factorial(N)*modInverse(N−i)*modInverse(i)}$

The algorithm can be achieved dividing it into three sections:

  • To check for every possibility of number of times x repeats in the number

    We can simply iterate in a for loop from i=0 to i<=N, where N is the required size of the number to consider every possible case for the number of times x repeats in the number.

    We can check the sum of the number for each case using the formula (x*i + (N−i)*y), where i is the number of times x repeats in the number which ranges from [0,N].

  • To Check if the sum of the number contains only x and y as its digits

    Since the sum of the number formed using x and y digits can be calculated as given below:

    sum=(x*i + (N−i)*y), where i is the number of times x appears in the number and (N−i) represents the number of times y appears in the number.

    We will create a function to check the sum of the number. In the function,we will iterate in a while loop until the sum becomes 0 and check for every digit of the sum using the modulo operator.

    The sum mod 10 will give the last digit of the number, so we will check if the last digit of the number is equal to x or y. If it is equal to x or y, we will just divide the number by 10 to check for the next digit until the sum becomes 0. If each digit of the sum is either x or y then we will return true. If any of the digits of the number is not equal to x or y, we will return false.

  • To find the number of ways to form the number using i times x digit if the sum has only x and y digits

    While checking for every possible value of i where i represents number of times x appears in the N−sized number, if the sum of digits of the number formed by i times x digit and (N−i) times y digit has only digits x and y, we will calculate the number of ways to form the distinct number using i times x digit and (N−i) times y digit using combinatorics.

    The formula to find the number of ways of arranging i times x digit in the number to form the distinct numbers is $\mathrm{^NC_{i}=\frac{N!}{(N-i)!*i!}}$.Since, $\mathrm{^NC_{i}=^NC_{N-i}}$ we can calculate either of them.

    $\mathrm{^NC_{i}=factorial}$

    For calculating the number of ways, we will precompute the value of factorial and inverse factorials from 0 to maximum value of N i.e. 10^6 and store them in an array of size 10^6+1.

    The modInverse can be computed using the formula:

    modInverse(N!)= modInverse((N+1)!)*(N+1).

    Since we have the values for every factorial number possible we will compute the number of ways using the formula above. The factorial values could be large therefore we will store the value in modulo of 1e9+7.

    We will add the number of ways in which distinct numbers can be formed using a particular number of digits and add them in the answer, further checking for every possible number of digits and finding the count of numbers.

    We can find the count of numbers formed by given digits of N size such that the sum of digits of the formed numbers have only given digits using this algorithm. We will use the algorithm in our approach in order to solve the problem.

Approach

The steps to be followed to implement the algorithm in our approach to find the count of number are given below:

  • We will create a function to count the numbers formed using the given digits such that the sum of digits only has the given digits.

  • We will iterate in a for loop from i=0 to i<=N to check for every number with i times x digit and (N−i) times y digit.

  • If the sum of the digits of the number with i times x and (N−i) times y digit does contains only x and y as its digit, we will calculate the number of ways we can form distinct numbers of size N with i times x digit using the formula $\mathrm{^NC_{i}}$

Example

//function to count the number formed by given digits whose sum having the given digit
#include <bits/stdc++.h>

using namespace std;

const int n=1000001;

const int mod=1e9+7;

//initialising array to store values of factorial and inverseFactorial till n
int fac[n]={0};
int inverseFac[n]={0};

//to calculate the inverse factorial of the last index
int lastFactorialIndex(int n,int m)
{
  int p=m;
  int a=1,b=0;
  if(m==1)
  {
      return 0;
  }
    while(n>1)
    {
      int quotient=n/m;
      
      int temp=m;
      
      m=n%m;
      
      n=temp;
      temp=b;
      
      b=a-quotient*b;
      
      a=temp;
    }

    if(a<0)
    {
     a=a+p; 
    }
	return a;
    
}

//function to store the values of factorial in the array
void factorial()
{
   fac[0]=1;
   
   fac[1]=1;
   
   for(int i=2;i<n;i++)
   {
       //since n!=n*(n-1)!
       fac[i]=(long long)fac[i-1]*i%mod;
   }
   
  
}

//function to calculate the inverse factorials for all values till n
void inverseFactorial()
{
    inverseFac[0]=1;
    inverseFac[1]=1;
    //calling the function to calculate the inverse factorial of last index
    inverseFac[n-1]=lastFactorialIndex(fac[n-1],mod);
    
    for(int i=n-2;i>=2;i--)
    {
        //inverse(i!)=inverse((i+1)!)*(i+1)
       inverseFac[i]=((long long)inverseFac[i+1]*(long long)(i+1)) % mod; 
    }
}

//function to check if the sum has only digits x and y
bool cal(int sum,int x,int y)
{

    if(sum==0)
    {
      return false;  
    }
    
    while(sum>0)
    {
        if((sum%10)!=x && (sum%10)!=y) //checking each digit of the number
        {
         return false;   
        }
        sum=sum/10;
    }
    return true;
}

//function give the number of ways of distinct numbers can be formed using i times x digit
long long int combinatorics(int n,int r)
{
    //using the formula nCr= factorial(n) * modInverse(n-r)*modInverse(r) % mod
 long long int ways= (long long)fac[n]*(long long)inverseFac[r] %mod *(long long)inverseFac[n-r]%mod;
 
  return ways;
}

// function to calculate count of numbers
int count_numbers(int m,int x,int y)
{
    //if both the digits are equal
    if(x==y)
    {
        if(cal(m*x,x,y)==true){
           return 1; 
        }
        else{
        return 0;
        }
    }
    int count=0;
    for(int i=0;i<=m;i++)
    {
        if(cal(i*x + (m-i)*y,x,y)) //checking for each possible case of x digit appearing i times
        {
           //if sum has the digits x and y, calculate the number of ways
            count = (count + combinatorics(m,i)) % mod; 
        }
        
    }
    return count;
}

int main()
{
    //calling the function to pregenerate factorials and inverse factorials
factorial();
inverseFactorial();

int m=188;
int x=8;
int y=4;
cout<<count_numbers(m,x,y);

return 0;
}

Output

917113677 

Time complexity− O(n*logn)

Space complexity − O(n) , because we used array of size n to store values of factorial and inverse factorial.

Conclusion

The article discusses the issue of determining the number of numbers that may be generated using the provided two digits such that the number's sum of digits only contains the digits x and y. Instead of examining each number, we used the combinatorics principle to determine the total number of numbers. The effective C++ solution was thoroughly explained.

After reading this post, I hope that you have a better understanding of the issue and the solution.

Updated on: 28-Aug-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements