📜  计算所有N位数为X的倍数的数字

📅  最后修改于: 2021-06-25 14:12:40             🧑  作者: Mango

给定两个整数NX ,任务是查找所有可能的N位数字的计数,其每一位数字都是X的倍数

例子 :

天真的方法:最简单的方法是对所有N位数字进行迭代,即在[10 N – 1,10 N – 1]范围内,对于每个数字,请检查该数字的每个数字是否为X的倍数。增加此类数字的数量并打印最终数量。

时间复杂度: O((10 N – 10 N – 1 )* N)
辅助空间: O(1)

高效方法:请按照以下步骤解决问题:

  1. 计算X的倍数的总位数,将其表示为total_Multiples
  2. 通过将上述所有数字排列在不同的位置以形成N位数字,可以获得所需的计数。
  1. 从上图可以看出,如果MX的所有倍数的计数,则所需的N位数字的计数(对于N> 1 )等于(M – 1)* M N – 1
  2. 对于一位数字,答案是M。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#define ll long long
 
// Function to calculate x^n
// using binary-exponentiation
ll power(ll x, ll n)
{
    // Stores the resultant power
    ll temp;
 
    if (n == 0)
        return 1;
 
    // Stores the value of x^(n/2)
    temp = power(x, n / 2);
    if (n % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}
 
// Function to count all N-digit numbers
// whose digits are multiples of x
ll count_Total_Numbers(ll n, ll x)
{
    ll total, multiples = 0;
 
    // Count all digits which
    // are multiples of x
    for (int i = 0; i < 10; i++) {
 
        // Check if current number
        // is a mutiple of X
        if (i % x == 0)
 
            // Increase count of multiples
            multiples++;
    }
 
    // Check if it's a 1 digit number
    if (n == 1)
        return multiples;
 
    // Count the total numbers
    total = (multiples - 1)
            * power(multiples, n - 1);
 
    // Return the total numbers
    return total;
}
 
// Driver Code
int main()
{
    // Given N and X
    ll N = 1, X = 3;
 
    // Function Call
    printf("%lld ",
           count_Total_Numbers(N, X));
 
    return 0;
}


Java
// Java program for
// the above approach
class GFG{
 
// Function to calculate x^n
// using binary-exponentiation
static int power(int x, int n)
{
   
  // Stores the resultant power
  int temp;
 
  if (n == 0)
    return 1;
 
  // Stores the value of x^(n/2)
  temp = power(x, n / 2);
  if (n % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
 
// Function to count aint N-digit
// numbers whose digits are multiples
// of x
static int count_Total_Numbers(int n,
                               int x)
{
  int total, multiples = 0;
 
  // Count aint digits which
  // are multiples of x
  for (int i = 0; i < 10; i++)
  {
     
    // Check if current number
    // is a mutiple of X
    if (i % x == 0)
 
      // Increase count of multiples
      multiples++;
  }
 
  // Check if it's a 1 digit number
  if (n == 1)
    return multiples;
 
  // Count the total numbers
  total = (multiples - 1) *
           power(multiples, n - 1);
 
  // Return the total numbers
  return total;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given N and X
  int N = 1, X = 3;
 
  // Function Call
  System.out.printf("%d ",
                    count_Total_Numbers(N, X));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to calculate x^n
# using binary-exponentiation
def power(x, n):
     
    # Stores the resultant power
    temp = []
 
    if (n == 0):
        return 1
 
    # Stores the value of x^(n/2)
    temp = power(x, n // 2)
    if (n % 2 == 0):
        return temp * temp
    else:
        return x * temp * temp
 
# Function to count aN-digit numbers
# whose digits are multiples of x
def count_Total_Numbers(n, x):
     
    total, multiples = 0, 0
 
    # Count adigits which
    # are multiples of x
    for i in range(10):
 
        # Check if current number
        # is a mutiple of X
        if (i % x == 0):
 
            # Increase count of multiples
            multiples += 1
 
    # Check if it's a 1 digit number
    if (n == 1):
        return multiples
 
    # Count the total numbers
    total = ((multiples - 1) *
     power(multiples, n - 1))
 
    # Return the total numbers
    return total
 
# Driver Code
if __name__ == '__main__':
     
    # Given N and X
    N = 1
    X = 3
 
    # Function call
    print(count_Total_Numbers(N, X))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate x^n
// using binary-exponentiation
static int power(int x, int n)
{
     
    // Stores the resultant power
    int temp;
     
    if (n == 0)
        return 1;
     
    // Stores the value of x^(n/2)
    temp = power(x, n / 2);
     
    if (n % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}
 
// Function to count aint N-digit
// numbers whose digits are multiples
// of x
static int count_Total_Numbers(int n,
                               int x)
{
    int total, multiples = 0;
     
    // Count aint digits which
    // are multiples of x
    for(int i = 0; i < 10; i++)
    {
         
        // Check if current number
        // is a mutiple of X
        if (i % x == 0)
     
        // Increase count of multiples
        multiples++;
    }
     
    // Check if it's a 1 digit number
    if (n == 1)
        return multiples;
     
    // Count the total numbers
    total = (multiples - 1) *
             power(multiples, n - 1);
     
    // Return the total numbers
    return total;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and X
    int N = 1, X = 3;
     
    // Function call
    Console.Write(count_Total_Numbers(N, X));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
4

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