📌  相关文章
📜  查询以给定范围内的数字进行计数,该数字可被其所有数字整除

📅  最后修改于: 2021-04-17 18:49:58             🧑  作者: Mango

给定一个二维数组arr [] [] ,每行以查询{L,R}的形式,任务是对范围[L,R]中的数字进行计数,以使该数字可被其所有非整数整除。 -零位数。

例子:

天真的方法:解决此问题的最简单方法是遍历数组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.


输出:
5 1

时间复杂度: O(N + Max),其中Max是arr [i] [1]的最大值
辅助空间: O(N)