📌  相关文章
📜  查询以其十进制或八进制表示形式包含数字K的范围内的数字进行计数

📅  最后修改于: 2021-04-17 19:24:34             🧑  作者: Mango

给定一个整数K和一个由{L,R}类型的N个查询组成的数组Q [] [] ,每个查询的任务是打印范围[L,R]中不包含整数的数字计数十进制或八进制表示形式的数字K。

例子:

天真的方法:这个想法是遍历每个查询的范围[L,R] ,并找到那些其十进制和八进制表示不包含数字K的数字。打印这些数字的计数。
时间复杂度: O(N 2 *(log 10 (N)+ log 8 (N)))
辅助空间: O(N)

高效的方法:为了优化上述方法,我们的想法是使用Prefix Sum技术预先计算所有此类数字的计数。请按照以下步骤解决问题:

  • 初始化一个数组,例如pref [] ,以存储范围为[0,i]的数字计数,该数字的八进制或十进制表示形式包含数字K。
  • 现在遍历范围[0,1e6]并执行以下步骤:
    • 如果数字以八进制表示形式或十进制表示形式包含数字K ,则将pref [i]更新为pref [i – 1] +1
    • 否则,将pref [i]更新为pref [i – 1]
  • 遍历给定的查询并将每个查询的计数打印为

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the given digit
// 'K' is present in the decimal and octal
// representations of num or not
bool contains(int num, int K, int base)
{
    // Stores if the digit exists
    // or not
    bool isThere = 0;
 
    // Iterate till nums is non-zero
    while (num) {
 
        // Find the remainder
        int remainder = num % base;
 
        // If the remainder is K
        if (remainder == K) {
            isThere = 1;
        }
 
        num /= base;
    }
 
    return isThere;
}
 
// Function to count the numbers in the
// range [1, N] such that it doesn't
// contain the digit 'K' in its decimal
// and octal representation
void count(int n, int k,
           vector > v)
{
 
    // Stores count of numbers in the
    // range [0, i] that contains the
    // digit 'K' in its octal or
    // decimal representation
    int pref[1000005] = { 0 };
 
    // Traverse the range [0, 1e6 + 5]
    for (int i = 1; i < 1e6 + 5; i++) {
 
        // Check if i contains the digit
        // 'K' in its decimal or
        // octal representation
        bool present
            = contains(i, k, 10)
              || contains(i, k, 8);
 
        // Update pref[i]
        pref[i] += pref[i - 1] + present;
    }
 
    // Print the answer of queries
    for (int i = 0; i < n; ++i) {
 
        cout << v[i][1] - v[i][0] + 1
                    - (pref[v[i][1]]
                       - pref[v[i][0] - 1])
             << ' ';
    }
}
 
// Driver Code
int main()
{
    int K = 7;
    vector > Q = { { 2, 5 },
                               { 1, 15 } };
    int N = Q.size();
 
    // Function Call
    count(N, K, Q);
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to check if the given digit
  // 'K' is present in the decimal and octal
  // representations of num or not
  static boolean contains(int num, int K, int Base)
  {
 
    // Stores if the digit exists
    // or not
    boolean isThere = false;
 
    // Iterate till nums is non-zero
    while (num > 0)
    {
 
      // Find the remainder
      int remainder = num % Base;
 
      // If the remainder is K
      if (remainder == K)
      {
        isThere = true;
      }
 
      num /= Base;
    }
 
    return isThere;
  }
 
  // Function to count the numbers in the
  // range [1, N] such that it doesn't
  // contain the digit 'K' in its decimal
  // and octal representation
  static void count(int n, int k,
                    Vector > v)
  {
 
    // Stores count of numbers in the
    // range [0, i] that contains the
    // digit 'K' in its octal or
    // decimal representation
    int[] pref = new int[1000005];
 
    // Traverse the range [0, 1e6 + 5]
    for (int i = 1; i < 1e6 + 5; i++) {
 
      // Check if i contains the digit
      // 'K' in its decimal or
      // octal representation
      boolean present
        = contains(i, k, 10)
        || contains(i, k, 8);
 
      // Update pref[i]
      if(present)
      {
        pref[i] += pref[i - 1] + 1;
      }
    }
 
    // Print the answer of queries
    System.out.print((v.get(0).get(1) - v.get(0).get(0) +
                      1 - (pref[v.get(0).get(1)] -
                           pref[v.get(0).get(0) - 1])) + " ");
    System.out.print((v.get(1).get(1) - v.get(1).get(0) -
                      (pref[v.get(1).get(1)] -
                       pref[v.get(1).get(0) - 1])) + " ");
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int K = 7;
    Vector > Q = new Vector >();
    Q.add(new Vector());
    Q.get(0).add(2);
    Q.get(0).add(5);
    Q.add(new Vector());
    Q.get(1).add(1);
    Q.get(1).add(15);
 
    int N = Q.size();
 
    // Function Call
    count(N, K, Q);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Python3
# Python3 program for the above approach
 
# Function to check if the given digit
# 'K' is present in the decimal and octal
# representations of num or not
def contains(num, K, base):
   
    # Stores if the digit exists
    # or not
    isThere = 0
 
    # Iterate till nums is non-zero
    while (num):
 
        # Find the remainder
        remainder = num % base
 
        # If the remainder is K
        if (remainder == K):
            isThere = 1
        num //= base
    return isThere
 
# Function to count the numbers in the
# range [1, N] such that it doesn't
# contain the digit 'K' in its decimal
# and octal representation
def count(n, k, v):
 
    # Stores count of numbers in the
    # range [0, i] that contains the
    # digit 'K' in its octal or
    # decimal representation
    pref = [0]*1000005
 
    # Traverse the range [0, 1e6 + 5]
    for i in range(1, 10 ** 6 + 5):
 
        # Check if i contains the digit
        # 'K' in its decimal or
        # octal representation
        present = contains(i, k, 10) or contains(i, k, 8)
 
        # Update pref[i]
        pref[i] += pref[i - 1] + present
 
    # Prthe answer of queries
    for i in range(n):
        print(v[i][1] - v[i][0] + 1- (pref[v[i][1]]- pref[v[i][0] - 1]),end=" ")
 
# Driver Code
if __name__ == '__main__':
    K = 7
    Q = [ [ 2, 5 ],
       [ 1, 15 ] ]
        
    N = len(Q)
     
    # Function Call
    count(N, K, Q)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to check if the given digit
  // 'K' is present in the decimal and octal
  // representations of num or not
  static bool contains(int num, int K, int Base)
  {
     
    // Stores if the digit exists
    // or not
    bool isThere = false;
 
    // Iterate till nums is non-zero
    while (num > 0)
    {
 
      // Find the remainder
      int remainder = num % Base;
 
      // If the remainder is K
      if (remainder == K)
      {
        isThere = true;
      }
 
      num /= Base;
    }
 
    return isThere;
  }
 
  // Function to count the numbers in the
  // range [1, N] such that it doesn't
  // contain the digit 'K' in its decimal
  // and octal representation
  static void count(int n, int k,
                    List > v)
  {
 
    // Stores count of numbers in the
    // range [0, i] that contains the
    // digit 'K' in its octal or
    // decimal representation
    int[] pref = new int[1000005];
 
    // Traverse the range [0, 1e6 + 5]
    for (int i = 1; i < 1e6 + 5; i++) {
 
      // Check if i contains the digit
      // 'K' in its decimal or
      // octal representation
      bool present
        = contains(i, k, 10)
        || contains(i, k, 8);
 
      // Update pref[i]
      if(present)
      {
        pref[i] += pref[i - 1] + 1;
      }
    }
 
    // Print the answer of queries
    Console.Write((v[0][1] - v[0][0] + 1 - (pref[v[0][1]] - pref[v[0][0] - 1])) + " ");
    Console.Write((v[1][1] - v[1][0] - (pref[v[1][1]] - pref[v[1][0] - 1])) + " ");
  }
 
  // Driver code
  static void Main()
  {
    int K = 7;
    List > Q = new List>();
    Q.Add(new List());
    Q[0].Add(2);
    Q[0].Add(5);
    Q.Add(new List());
    Q[1].Add(1);
    Q[1].Add(15);
 
    int N = Q.Count;
 
    // Function Call
    count(N, K, Q);
  }
}
 
// This code is contributed by divyesh072019.


输出:
4 13

时间复杂度: O(N *(log 10 (N)+ log 8 (N)))
辅助空间: O(N)