# 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++

 `// C++ Program for the above approach` `#include ` `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;` `}`

## 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`

## 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`

## 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`

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.