给定一个二维数组arr[][] ,每行的形式为查询{ L, R } ,任务是计算范围[L, R]中的数字,使得该数字可以被其所有非-零位。
例子:
Input: arr[][] ={ {1, 5}, {12, 14} }
Output: 5 1
Explanation:
Query1: All the numbers in the range [1, 5] are divisible by their digits.
Query2: Numbers in the range [12, 14] which are divisible by all of its digits is 12 only.
Input: arr[][] = { {1, 20} }
Output:14
Explanation:
Numbers in the range [1, 20] which are divisible by all of its non-zero digits are: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 20}
朴素的方法:解决这个问题的最简单的方法是遍历数组arr[][]并且对于数组的每第i行,迭代范围[L, R] 。对于范围内的每个元素,检查该数字是否可以被其所有非零数字整除。如果发现为真,则增加计数。最后,打印获得的计数。
时间复杂度: O(N * (R – L)),其中 N 是行数
辅助空间: O(1)
有效的方法:可以通过找到arr[i][1]的最大可能值来优化上述方法,并使用 Prefix Sum 技术预先计算可被其非零数字整除的数字的计数。请按照以下步骤解决问题:
- 初始化一个变量,比如Max ,以存储arr[i][1]的最大可能值。
- 初始化一个数组,比如prefCntDiv[] ,其中prefCntDiv[i]存储范围[1, i] 中的数字计数,该范围可被其非零数字整除。
- 迭代范围[1, Max] 。对于每个第i次迭代,检查i是否可以被其所有非零数字整除。如果发现为真,则更新prefCntDiv[i] = prefCntDiv[i – 1] + 1 。
- 否则,更新prefCntDiv[i] = prefCntDiv[i – 1] 。
- 遍历数组arr[] 。对于数组的每第i 行,打印prefCntDiv[arr[i][1]] – prefCntDiv[arr[i][0] – 1]。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
#define Max 1000005
// Function to check if a number is divisible
// by all of its non-zero digits or not
bool CheckDivByAllDigits(int number)
{
// Stores the number
int n = number;
// Iterate over the digits
// of the numbers
while (n > 0) {
// If digit of number
// is non-zero
if (n % 10)
// If number is not divisible
// by its current digit
if (number % (n % 10)) {
return false;
}
// Update n
n /= 10;
}
return true;
}
// Function to count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
void cntNumInRang(int arr[][2], int N)
{
// Stores count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
int prefCntDiv[Max] = { 0 };
// Iterate over the range [1, Max]
for (int i = 1; i <= Max; i++) {
// Update
prefCntDiv[i] = prefCntDiv[i - 1]
+ (CheckDivByAllDigits(i));
}
// Traverse the array, arr[]
for (int i = 0; i < N; i++)
cout << (prefCntDiv[arr[i][1]]
- prefCntDiv[arr[i][0] - 1])
<< " ";
}
// Driver Code
int main()
{
int arr[][2] = { { 1, 5 }, { 12, 14 } };
int N = sizeof(arr) / sizeof(arr[0]);
cntNumInRang(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG
{
public static int Max = 1000005;
// Function to check if a number is divisible
// by all of its non-zero digits or not
static boolean CheckDivByAllDigits(int number)
{
// Stores the number
int n = number;
// Iterate over the digits
// of the numbers
while (n > 0)
{
// If digit of number
// is non-zero
if (n % 10 != 0)
// If number is not divisible
// by its current digit
if (number % (n % 10) != 0)
{
return false;
}
// Update n
n /= 10;
}
return true;
}
// Function to count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
static void cntNumInRang(int arr[][], int N)
{
// Stores count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
int prefCntDiv[] = new int[Max + 1];
// Iterate over the range [1, Max]
for (int i = 1; i <= Max; i++)
{
int ans = 0;
if (CheckDivByAllDigits(i))
ans = 1;
// Update
prefCntDiv[i] = prefCntDiv[i - 1] + ans;
}
// Traverse the array, arr[]
for (int i = 0; i < N; i++)
System.out.print((prefCntDiv[arr[i][1]]
- prefCntDiv[arr[i][0] - 1])
+ " ");
}
// Driver Code
public static void main(String[] args)
{
int arr[][] = { { 1, 5 }, { 12, 14 } };
int N = arr.length;
cntNumInRang(arr, N);
}
}
// This code is contributed by Dharanendra L V
Python3
# Python3 program to implement
# the above approach
# Function to check if a number is divisible
# by all of its non-zero digits or not
def CheckDivByAllDigits(number):
# Stores the number
n = number
# Iterate over the digits
# of the numbers
while (n > 0):
# If digit of number
# is non-zero
if (n % 10):
# If number is not divisible
# by its current digit
if (number % (n % 10)):
return False
# Update n
n //= 10
return True
# Function to count of numbers which are
# divisible by all of its non-zero
# digits in the range [1, i]
def cntNumInRang(arr, N):
global Max
# Stores count of numbers which are
# divisible by all of its non-zero
# digits in the range [1, i]
prefCntDiv = [0]*Max
# Iterate over the range [1, Max]
for i in range(1, Max):
# Update
prefCntDiv[i] = prefCntDiv[i - 1] + (CheckDivByAllDigits(i))
# Traverse the array, arr[]
for i in range(N):
print(prefCntDiv[arr[i][1]]- prefCntDiv[arr[i][0] - 1], end = " ")
# Driver Code
if __name__ == '__main__':
arr =[ [ 1, 5 ], [12, 14]]
Max = 1000005
N = len(arr)
cntNumInRang(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program to implement
// the above approach
using System;
class GFG
{
public static int Max = 1000005;
// Function to check if a number is divisible
// by all of its non-zero digits or not
static bool CheckDivByAllDigits(int number)
{
// Stores the number
int n = number;
// Iterate over the digits
// of the numbers
while (n > 0) {
// If digit of number
// is non-zero
if (n % 10 != 0)
// If number is not divisible
// by its current digit
if (number % (n % 10) != 0)
{
return false;
}
// Update n
n /= 10;
}
return true;
}
// Function to count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
static void cntNumInRang(int[, ] arr, int N)
{
// Stores count of numbers which are
// divisible by all of its non-zero
// digits in the range [1, i]
int[] prefCntDiv = new int[Max + 1];
// Iterate over the range [1, Max]
for (int i = 1; i <= Max; i++) {
int ans = 0;
if (CheckDivByAllDigits(i))
ans = 1;
// Update
prefCntDiv[i] = prefCntDiv[i - 1] + ans;
}
// Traverse the array, arr[]
for (int i = 0; i < N; i++)
Console.Write((prefCntDiv[arr[i, 1]]
- prefCntDiv[arr[i, 0] - 1])
+ " ");
}
// Driver Code
public static void Main(string[] args)
{
int[, ] arr = { { 1, 5 }, { 12, 14 } };
int N = arr.GetLength(0);
cntNumInRang(arr, N);
}
}
// This code is contributed by chitranayal.
Javascript
5 1
时间复杂度: O(N + Max),其中 Max 是 arr[i][1] 的最大值
辅助空间: O(N)