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
**0**s.

**Note: **Since the answer can be very large, print the answer modulo **10 ^{9 }+ 7**.

**Examples:**

Input:N = 2, X = 1, Y = 2Output:2Explanation:There are only two possible numbers 12 and 21.

Input:N = 10, X = 3, Y = 4100172994Output:

**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**10**^{N} - Total
**N-**digit numbers that can be formed using digits**{0 – 9} – { X }**is**9**^{N} - Total
**N**-digit numbers that can be formed using digit**{0 – 9} – {X, Y}**is**8**^{N} - 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**10**^{N }– 2 * 9^{N}+ 8^{N}.

Below is the implementation of the above approach:

## C++

`// C++ Program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `const` `int` `mod = 1e9 + 7;` `// Function for calculate` `// (x ^ y) % mod in O(log y)` `long` `long` `power(` `int` `x, ` `int` `y)` `{` ` ` `// Base Condition` ` ` `if` `(y == 0)` ` ` `return` `1;` ` ` `// Transition state of` ` ` `// power Function` ` ` `long` `long` `int` `p` ` ` `= power(x, y / 2) % mod;` ` ` `p = (p * p) % mod;` ` ` `if` `(y & 1) {` ` ` `p = (x * p) % mod;` ` ` `}` ` ` `return` `p;` `}` `// Function for counting total numbers` `// that can be formed such that digits` `// X, Y are present in each number` `int` `TotalNumber(` `int` `N)` `{` ` ` `// Calculate the given expression` ` ` `int` `ans = (power(10, N)` ` ` `- 2 * power(9, N)` ` ` `+ power(8, N) + 2 * mod)` ` ` `% mod;` ` ` `// Return the final answer` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 10, X = 3, Y = 4;` ` ` `cout << TotalNumber(N) << endl;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `static` `int` `mod = (` `int` `)(1e9 + ` `7` `);` `// Function for calculate` `// (x ^ y) % mod in O(log y)` `static` `long` `power(` `int` `x, ` `int` `y)` `{` ` ` `// Base Condition` ` ` `if` `(y == ` `0` `)` ` ` `return` `1` `;` ` ` `// Transition state of` ` ` `// power Function` ` ` `int` `p = (` `int` `)(power(x, y / ` `2` `) % mod);` ` ` `p = (p * p) % mod;` ` ` `if` `(y % ` `2` `== ` `1` `)` ` ` `{` ` ` `p = (x * p) % mod;` ` ` `}` ` ` `return` `p;` `}` `// Function for counting total numbers` `// that can be formed such that digits` `// X, Y are present in each number` `static` `int` `TotalNumber(` `int` `N)` `{` ` ` ` ` `// Calculate the given expression` ` ` `int` `ans = (` `int` `)((power(` `10` `, N) - ` `2` `* ` ` ` `power(` `9` `, N) + ` ` ` `power(` `8` `, N) + ` ` ` `2` `* mod) % mod);` ` ` `// Return the final answer` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `10` `, X = ` `3` `, Y = ` `4` `;` ` ` ` ` `System.out.print(TotalNumber(N) + ` `"\n"` `);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `mod ` `=` `1e9` `+` `7` `# Function for calculate` `# (x ^ y) % mod in O(log y)` `def` `power(x, y):` ` ` `# Base Condition` ` ` `if` `(y ` `=` `=` `0` `):` ` ` `return` `1` ` ` `# Transition state of` ` ` `# power Function` ` ` `p ` `=` `power(x, y ` `/` `/` `2` `) ` `%` `mod` ` ` `p ` `=` `(p ` `*` `p) ` `%` `mod` ` ` `if` `(y & ` `1` `):` ` ` `p ` `=` `(x ` `*` `p) ` `%` `mod` ` ` `return` `p` `# Function for counting total numbers` `# that can be formed such that digits` `# X, Y are present in each number` `def` `TotalNumber(N):` ` ` `# Calculate the given expression` ` ` `ans ` `=` `(power(` `10` `, N) ` `-` `2` `*` ` ` `power(` `9` `, N) ` `+` ` ` `power(` `8` `, N) ` `+` `2` `*` `mod) ` `%` `mod` ` ` `# Return the final answer` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `10` ` ` `X ` `=` `3` ` ` `Y ` `=` `4` ` ` ` ` `print` `(TotalNumber(N))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `static` `int` `mod = (` `int` `)(1e9 + 7);` `// Function for calculate` `// (x ^ y) % mod in O(log y)` `static` `long` `power(` `int` `x, ` `int` `y)` `{` ` ` `// Base Condition` ` ` `if` `(y == 0)` ` ` `return` `1;` ` ` `// Transition state of` ` ` `// power Function` ` ` `int` `p = (` `int` `)(power(x, ` ` ` `y / 2) % mod);` ` ` `p = (p * p) % mod;` ` ` `if` `(y % 2 == 1)` ` ` `{` ` ` `p = (x * p) % mod;` ` ` `}` ` ` `return` `p;` `}` `// Function for counting ` `// total numbers that can be ` `// formed such that digits` `// X, Y are present in each number` `static` `int` `TotalNumber(` `int` `N)` `{ ` ` ` `// Calculate the given expression` ` ` `int` `ans = (` `int` `)((power(10, N) - 2 * ` ` ` `power(9, N) + ` ` ` `power(8, N) + ` ` ` `2 * mod) % mod);` ` ` `// Return the ` ` ` `// readonly answer` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 10;` ` ` `Console.Write(TotalNumber(N) + ` `"\n"` `);` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output**

100172994

* Time Complexity: *O(

*log N)*

**Auxiliary Space:**O(1)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Construct an AP series consisting of A and B having minimum possible Nth term
- Length of largest subsequence consisting of a pair of alternating digits
- Smallest power of 2 consisting of N digits
- Count of subarrays consisting of only prime numbers
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers from given range having odd digits at odd places and even digits at even places
- Minimum digits to be removed to make either all digits or alternating digits same
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of N size strings consisting of at least one vowel and one consonant
- Count different numbers possible using all the digits their frequency times
- Maximum count of sub-strings of length K consisting of same characters
- Count of binary strings of given length consisting of at least one 1
- Count of substrings consisting only of vowels
- Count of substrings consisting of even number of vowels
- Count of subsequences consisting of the same element
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count N-length strings consisting only of vowels sorted lexicographically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.