📜  计算范围为立方体为回文式的范围内的数字

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

给定一个由N个形式为{L,R}的查询组成的数组Q [] [] ,每个查询的任务是从范围[L,R]中查找其总数为回文的数字总数。

例子:

方法:解决问题的最简单方法是使用“包含-排除原理”和“前缀和数组”技术来解决此问题。请按照以下步骤解决给定的问题:

  • 初始化数组arr [] ,以在i存储 索引,无论i的多维数据集是否为回文。
  • 遍历数组arr []并且每隔i遍历一次 索引,检查i的多维数据集是否是回文。
    • 如果发现为真,则设置arr [i] = 1
    • 否则,设置arr [i] = 0
  • 将数组arr []转换为前缀和数组。
  • 遍历数组Q [] [] ,并通过计算arr [R] – arr [L-1]来计算范围为[L,R]的立方数为回文数的数字。

下面是上述方法的实现。

C++
// C++ program of the above approach
#include 
using namespace std;
 
int arr[10005];
 
// Function to check if n is
// a pallindrome number or not
int isPalindrome(int n)
{
    // Temporarily store n
    int temp = n;
 
    // Stores reverse of n
    int res = 0;
 
    // Iterate until temp reduces to 0
    while (temp != 0) {
        // Extract the last digit
        int rem = temp % 10;
 
        // Add to the start
        res = res * 10 + rem;
 
        // Remove the last digit
        temp /= 10;
    }
 
    // If the number and its
    // reverse are equal
    if (res == n) {
        return 1;
    }
 
    // Otherwise
    else
        return 0;
}
 
// Function to precompute and store
// the count of numbers whose cube
// is a palindrome number
void precompute()
{
    // Iterate upto 10^4
    for (int i = 1; i <= 10000; i++) {
 
        // Check if i*i*i is a
        // pallindrome or not
        if (isPalindrome(i * i * i))
            arr[i] = 1;
        else
            arr[i] = 0;
    }
 
    // Convert arr[] to prefix sum array
    for (int i = 1; i <= 10000; i++) {
        arr[i] = arr[i] + arr[i - 1];
    }
}
 
// Driver Code
int main()
{
    // Given queries
    vector > Q = { { 2, 7 }, { 10, 25 } };
 
    precompute();
 
    for (auto it : Q) {
 
        // Using inclusion-exclusion
        // principle, count required numbers
        cout << arr[it.second] - arr[it.first - 1] << "\n";
    }
 
    return 0;
}


Java
// Java program of the above approach
import java.io.*;
import java.util.*;
public class Pair {
  private final int key;
  private final int value;
 
  public Pair(int aKey, int aValue)
  {
    key = aKey;
    value = aValue;
  }
 
  public int key() { return key; }
  public int value() { return value; }
}
 
class GFG {
  static int[] arr = new int[10005];
 
  // Function to check if n is
  // a pallindrome number or not
  static int isPalindrome(int n)
  {
     
    // Temporarily store n
    int temp = n;
 
    // Stores reverse of n
    int res = 0;
 
    // Iterate until temp reduces to 0
    while (temp != 0)
    {
       
      // Extract the last digit
      int rem = temp % 10;
 
      // Add to the start
      res = res * 10 + rem;
 
      // Remove the last digit
      temp /= 10;
    }
 
    // If the number and its
    // reverse are equal
    if (res == n) {
      return 1;
    }
 
    // Otherwise
    else
      return 0;
  }
 
  // Function to precompute and store
  // the count of numbers whose cube
  // is a palindrome number
  static void precompute()
  {
     
    // Iterate upto 10^4
    for (int i = 1; i <= 10000; i++) {
 
      // Check if i*i*i is a
      // pallindrome or not
      if (isPalindrome(i * i * i)!= 0)
        arr[i] = 1;
      else
        arr[i] = 0;
    }
 
    // Convert arr[] to prefix sum array
    for (int i = 1; i <= 10000; i++) {
      arr[i] = arr[i] + arr[i - 1];
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given queries
    ArrayList Q = new ArrayList();
    Pair pair = new Pair(2, 7);
    Q.add(pair);
    Pair pair2 = new Pair(10, 25);
    Q.add(pair2);
 
    precompute();
 
    for (int i = 0; i < Q.size(); i++)  {
 
      // Using inclusion-exclusion
      // principle, count required numbers
      System.out.println(arr[Q.get(i).value()] - arr[Q.get(i).key()-1]);
    }
  }
}
 
// This code is contributed by Dharanendra L V


输出:
2
1

时间复杂度: O(N)
辅助空间: O(maxm),其中maxm表示查询中R的最大值