给定两个整数N和X ,任务是查找所有可能的N位数字的计数,其每一位数字都是X的倍数。
例子 :
Input: N = 1, X = 3
Output: 4
Explanation:
The single-digit numbers whose digits are multiple of 3 are 0, 3, 6 and 9.
So the answer will be 4.
Input: N = 2, X = 4
Output: 6
Explanation:
The two-digit numbers whose digits are multiple of 4 are 40, 44, 48, 80, 84, 88.
So the answer will be 6.
天真的方法:最简单的方法是对所有N位数字进行迭代,即在[10 N – 1,10 N – 1]范围内,对于每个数字,请检查该数字的每个数字是否为X的倍数。增加此类数字的数量并打印最终数量。
时间复杂度: O((10 N – 10 N – 1 )* N)
辅助空间: O(1)
高效方法:请按照以下步骤解决问题:
- 计算X的倍数的总位数,将其表示为total_Multiples 。
- 通过将上述所有数字排列在不同的位置以形成N位数字,可以获得所需的计数。
Illustration:
For example N = 2, X = 3
Multiples of 3 are S = { 0, 3, 6, 9 }
- To find all 2-digit numbers, arrange all the multiples of X to form a 2-digit number and count it.
- Arrange the numbers from the set S. Taking 0 for the most significant bit will result in 1-digit number, so there are 3 ways to arrange the most significant position of a number and there are 4 ways to arrange the last digit of a number whose digits are multiples of 3.
So total ways = 3*4 = 12 so the answer will be 12.
- 从上图可以看出,如果M是X的所有倍数的计数,则所需的N位数字的计数(对于N> 1 )等于(M – 1)* M N – 1 。
- 对于一位数字,答案是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)