给定三个整数N 、 X和Y ,任务是找到可以使用满足以下条件的数字0到9形成的N位数字的计数:
- 数字X和Y必须存在于其中。
- 该数字可能包含前导0 。
注意:由于答案可能非常大,打印答案模10 9 + 7 。
例子:
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
方法:思路是使用排列组合技术来解决问题。请按照以下步骤解决问题:
- 可能使用数字{0 – 9} 的总N位数字为10 N
- 可以使用数字{0 – 9} – { X }组成的总N位数字是9 N
- 可以使用数字{0 – 9} – {X, Y}组成的总共N位数字是8 N
- 包含数字X和Y 的总N位数字是所有可能的数字与不包含数字X或Y的数字之间的差,然后是包含除X和Y之外的所有数字的数字的总和。因此,答案是10 N – 2 * 9 N + 8 N。
下面是上述方法的实现:
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
Javascript
输出
100172994
时间复杂度: O( log N)
辅助空间: O(1)