Count N-digit numbers possible consisting of digits X and Y
Given three integers N, X, and Y, the task is to find the count of N-digit numbers that can be formed using digits 0 to 9 satisfying the following conditions:
- Digits X and Y must be present in them.
- The number may contain leading 0s.
Note: Since the answer can be very large, print the answer modulo 109 + 7.
Examples:
Input: N = 2, X = 1, Y = 2
Output: 2
Explanation:
There are only two possible numbers 12 and 21.
Input: N = 10, X = 3, Y = 4
Output: 100172994
Approach: The idea is to use permutation and combination techniques to solve the problem. Follow the steps below to solve the problem:
- Total N-digit numbers that possible using digits {0 – 9} is 10N
- Total N-digit numbers that can be formed using digits {0 – 9} – { X } is 9N
- Total N-digit numbers that can be formed using digit {0 – 9} – {X, Y} is 8N
- Total N-digit numbers that contain digit X and Y is the difference between all possible numbers and the numbers which do not contain digit X or Y followed by the summation of the numbers which contain all digits except X and Y. Hence, the answer is 10N – 2 * 9N + 8N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long power( int x, int y)
{
if (y == 0)
return 1;
long long int p
= power(x, y / 2) % mod;
p = (p * p) % mod;
if (y & 1) {
p = (x * p) % mod;
}
return p;
}
int TotalNumber( int N)
{
int ans = (power(10, N)
- 2 * power(9, N)
+ power(8, N) + 2 * mod)
% mod;
return ans;
}
int main()
{
int N = 10, X = 3, Y = 4;
cout << TotalNumber(N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mod = ( int )(1e9 + 7 );
static long power( int x, int y)
{
if (y == 0 )
return 1 ;
int p = ( int )(power(x, y / 2 ) % mod);
p = (p * p) % mod;
if (y % 2 == 1 )
{
p = (x * p) % mod;
}
return p;
}
static int TotalNumber( int N)
{
int ans = ( int )((power( 10 , N) - 2 *
power( 9 , N) +
power( 8 , N) +
2 * mod) % mod);
return ans;
}
public static void main(String[] args)
{
int N = 10 , X = 3 , Y = 4 ;
System.out.print(TotalNumber(N) + "\n" );
}
}
|
Python3
mod = 1e9 + 7
def power(x, y):
if (y = = 0 ):
return 1
p = power(x, y / / 2 ) % mod
p = (p * p) % mod
if (y & 1 ):
p = (x * p) % mod
return p
def TotalNumber(N):
ans = (power( 10 , N) - 2 *
power( 9 , N) +
power( 8 , N) + 2 * mod) % mod
return ans
if __name__ = = '__main__' :
N = 10
X = 3
Y = 4
print (TotalNumber(N))
|
C#
using System;
class GFG{
static int mod = ( int )(1e9 + 7);
static long power( int x, int y)
{
if (y == 0)
return 1;
int p = ( int )(power(x,
y / 2) % mod);
p = (p * p) % mod;
if (y % 2 == 1)
{
p = (x * p) % mod;
}
return p;
}
static int TotalNumber( int N)
{
int ans = ( int )((power(10, N) - 2 *
power(9, N) +
power(8, N) +
2 * mod) % mod);
return ans;
}
public static void Main(String[] args)
{
int N = 10;
Console.Write(TotalNumber(N) + "\n" );
}
}
|
Javascript
<script>
var mod = 1000000007;
function power(x, y)
{
if (y == 0)
return 1;
var p
= power(x, y / 2) % mod;
p = (p * p) % mod;
if (y & 1) {
p = (x * p) % mod;
}
return p;
}
function TotalNumber(N)
{
var ans = (power(10, N)
- 2 * power(9, N)
+ power(8, N) + 2 * mod)
% mod;
return ans;
}
var N = 10, X = 3, Y = 4;
document.write( TotalNumber(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...