📜  计算没有特定数字的n个数字

📅  最后修改于: 2021-04-27 23:42:37             🧑  作者: Mango

我们给了两个整数n和d,我们需要计算所有没有数字d的n个数字。
例子 :

Input : n = 2, d = 7
Output : 72
All two digit numbers that don't have
7 as digit are 10, 11, 12, 13, 14, 15,
16, 18, .....

Input : n = 3, d = 9
Output : 648

一个简单的解决方案是遍历所有d位数字。对于每个数字,请检查是否有x作为数字。
高效方法在此方法中,我们首先检查排除数字d是否为零或非零。如果为零,则我们有9个数字(1到9)作为第一个数字,否则我们有8个数字(x位数和0除外)。然后,对于所有其他数字,我们有9个选择,即(0到9,不包括d数字)。我们用n-1个数字作为第一个数字来简单地调用digitNumber函数,我们已经发现它可以是8或9并简单地乘以它。对于其他数字,我们检查是否位数是奇数或偶数,如果它是奇数我们称之为与第(n-1)/ 2位digitNumber函数和乘法它由9 oterwise我们称之为digitNumber函数具有n / 2个数字,并将它们存储在结果和取结果方块。
插图 :

Number from 1 to 7 excluding digit 9.
digits      multiple          number
1           8                   8
2           8*9                 72
3           8*9*9               648 
4           8*9*9*9             5832
5           8*9*9*9*9           52488
6           8*9*9*9*9*9         472392
7           8*9*9*9*9*9*9       4251528

如我们所见,在每一步中,我们的位数是数字的一半。假设我们用7(7)位数字从main调用函数的6(7-1)位数字。当我们将一半数字留下3(6/2)个数字时。因此,我们必须将3位数字的结果与其自身相乘,以获得6位数字的结果。同样,对于3位数字,我们有奇数位,也有奇数位。我们找到1((3-1)/ 2)位数字的结果,找到结果平方并乘以9,因为我们找到d-1位数字的结果。

C++
// C++ Implementation of above method
#include 
#define mod 1000000007
using namespace std;
 
// Finding number of possible number with
// n digits excluding a particular digit
long long digitNumber(long long n) {
 
  // Checking if number of digits is zero
  if (n == 0)
    return 1;
 
  // Checking if number of digits is one
  if (n == 1)
    return 9;
 
  // Checking if number of digits is odd
  if (n % 2) {
 
    // Calling digitNumber function
    // with (digit-1)/2 digits
    long long temp = digitNumber((n - 1) / 2) % mod;
    return (9 * (temp * temp) % mod) % mod;
  } else {
 
    // Calling digitNumber function
    // with n/2 digits
    long long temp = digitNumber(n / 2) % mod;
    return (temp * temp) % mod;
  }
}
 
int countExcluding(int n, int d)
{
  // Calling digitNumber function
  // Checking if excluding digit is
  // zero or non-zero
  if (d == 0)
    return (9 * digitNumber(n - 1)) % mod;
  else
    return (8 * digitNumber(n - 1)) % mod;
}
 
// Driver function to run above program
int main() {
 
  // Initializing variables
  long long d = 9;
  int n = 3;
  cout << countExcluding(n, d) << endl;
  return 0;
}


Java
// Java Implementation of above method
import java.lang.*;
 
class GFG {
     
static final int mod = 1000000007;
 
// Finding number of possible number with
// n digits excluding a particular digit
static int digitNumber(long n) {
 
    // Checking if number of digits is zero
    if (n == 0)
    return 1;
 
    // Checking if number of digits is one
    if (n == 1)
    return 9;
 
    // Checking if number of digits is odd
    if (n % 2 != 0) {
 
    // Calling digitNumber function
    // with (digit-1)/2 digits
    int temp = digitNumber((n - 1) / 2) % mod;
     
    return (9 * (temp * temp) % mod) % mod;
    }
    else {
 
    // Calling digitNumber function
    // with n/2 digits
    int temp = digitNumber(n / 2) % mod;
     
    return (temp * temp) % mod;
    }
}
 
static int countExcluding(int n, int d) {
     
    // Calling digitNumber function
    // Checking if excluding digit is
    // zero or non-zero
    if (d == 0)
    return (9 * digitNumber(n - 1)) % mod;
    else
    return (8 * digitNumber(n - 1)) % mod;
}
 
// Driver function to run above program
public static void main(String[] args) {
     
    // Initializing variables
    int d = 9;
    int n = 3;
    System.out.println(countExcluding(n, d));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python Implementation
# of above method
 
mod=1000000007
 
# Finding number of
# possible number with
# n digits excluding
# a particular digit
def digitNumber(n):
 
    # Checking if number
    # of digits is zero
    if (n == 0):
        return 1
  
    # Checking if number
    # of digits is one
    if (n == 1):
        return 9
  
    # Checking if number
    # of digits is odd
    if (n % 2!=0):
  
        # Calling digitNumber function
        # with (digit-1)/2 digits
        temp = digitNumber((n - 1) // 2) % mod
        return (9 * (temp * temp) % mod) % mod
    else:
  
        # Calling digitNumber function
        # with n/2 digits
        temp = digitNumber(n // 2) % mod
        return (temp * temp) % mod
  
def countExcluding(n,d):
 
    # Calling digitNumber function
    # Checking if excluding digit is
    # zero or non-zero
    if (d == 0):
        return (9 * digitNumber(n - 1)) % mod
    else:
        return (8 * digitNumber(n - 1)) % mod
     
# Driver code
 
d = 9
n = 3
print(countExcluding(n, d))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# Implementation of above method
using System;
 
class GFG {
     
    static int mod = 1000000007;
     
    // Finding number of possible number with
    // n digits excluding a particular digit
    static int digitNumber(long n) {
     
        // Checking if number of digits is zero
        if (n == 0)
            return 1;
     
        // Checking if number of digits is one
        if (n == 1)
            return 9;
     
        // Checking if number of digits is odd
        if (n % 2 != 0) {
     
            // Calling digitNumber function
            // with (digit-1)/2 digits
            int temp = digitNumber((n - 1) / 2) % mod;
             
            return (9 * (temp * temp) % mod) % mod;
        }
        else {
     
            // Calling digitNumber function
            // with n/2 digits
            int temp = digitNumber(n / 2) % mod;
             
            return (temp * temp) % mod;
        }
    }
     
    static int countExcluding(int n, int d) {
         
        // Calling digitNumber function
        // Checking if excluding digit is
        // zero or non-zero
        if (d == 0)
            return (9 * digitNumber(n - 1)) % mod;
        else
            return (8 * digitNumber(n - 1)) % mod;
    }
     
    // Driver function to run above program
    public static void Main() {
         
        // Initializing variables
        int d = 9;
        int n = 3;
         
        Console.WriteLine(countExcluding(n, d));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:
648

时间复杂度: O(log n)。