📌  相关文章
📜  计算可能由数字X和Y组成的N位数字

📅  最后修改于: 2021-04-23 08:17:45             🧑  作者: Mango

给定三个整数NXY ,任务是找到可以使用满足以下条件的数字09形成的N位数字的计数:

  • XY中必须存在数字。
  • 该数字可能包含前导0 s。

注意:由于答案可能非常大,请以10 9 + 7模输出答案。

例子:

方法:想法是使用置换和组合技术来解决该问题。请按照以下步骤解决问题:

  • 使用数字{0 – 9}可能产生的N位数字总数为10 N
  • 可以使用数字{0 – 9} – {X}形成的N位数字总数为9 N
  • 可以使用数字{0 – 9} – {X,Y}形成的N位数字总数为8 N
  • 包含数字XY的N位数字总数是所有可能的数字与不包含数字XY的数字之间的差,然后是包含除XY之外的所有数字的数字的总和。因此,答案是10 N – 2 * 9 N + 8N

下面是上述方法的实现:

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


输出
100172994


时间复杂度: O( log N)
辅助空间: O(1)