给定一个数组Q[][]由N 个形式为{L, R} 的查询组成,每个查询的任务是找到范围[L, R]中回文数的计数以及它们的总和数字是质数。
例子:
Input: Q[][] = {{5, 9}, {5, 22}}
Output:
2
3
Explanation:
Query 1: All palindrome numbers from the range [5, 9] having sum of their digits equal to a prime number are {5, 7}. Therefore, the count of elements is 2.
Query 2: All palindrome numbers from the range [5, 20] having sum of their digits equal to a prime number are {5, 7, 11}. Therefore, the count of elements is 2.
Input: Q[] = {{1, 101}, {13, 15}}
Output:
6
0
朴素方法:解决给定问题的最简单方法是对每个查询的范围[L, R]进行迭代,并打印这些回文数的计数,并且它们的数字之和是质数。
时间复杂度: O(N*(R – L))
辅助空间: O(1)
高效方法:上述方法可以通过使用包含-排除原理和前缀和的概念进行优化。请按照以下步骤解决给定的问题:
- 初始化数组arr[]以存储回文索引i的数字计数,并且它们的数字之和是每个第i个索引处的质数。
- 迭代范围[1, 10 5 ] ,对于每个元素X ,如果数字 X 是回文且数字之和为素数,则将arr[i]更新为1 。否则,设置arr[i] = 0 。
- 查找数组arr[]的前缀和数组。
- 现在,遍历数组Q[]并且对于每个查询{L, R} ,打印范围[L, R]中回文数字的总数,并且它们的数字之和是素数,如(arr[ R] – arr[L – 1]) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
int arr[100005];
// Function to check if the number N
// is palindrome or not
bool isPalindrome(int N)
{
// Store the value of N
int temp = N;
// Store the reverse of number N
int res = 0;
// Reverse temp and store in res
while (temp != 0) {
int rem = temp % 10;
res = res * 10 + rem;
temp /= 10;
}
// If N is the same as res, then
// return true
if (res == N) {
return true;
}
else {
return false;
}
}
// Function to find the sum of the
// digits of the number N
int sumOfDigits(int N)
{
// Stores the sum of the digits
int sum = 0;
while (N != 0) {
// Add the last digit of the
// number N to the sum
sum += N % 10;
// Remove the last digit
// from N
N /= 10;
}
// Return the resultant sum
return sum;
}
// Function to check if N is prime or not
bool isPrime(int n)
{
// If i is 1 or 0, then return false
if (n <= 1) {
return false;
}
// Check if i is divisible by any
// number in the range [2, n/2]
for (int i = 2; i <= n / 2; ++i) {
// If n is divisible by i
if (n % i == 0)
return false;
}
return true;
}
// Function to precompute all the numbers
// till 10^5 that are palindromic and
// whose sum of digits is prime numbers
void precompute()
{
// Iterate over the range 1 to 10 ^ 5
for (int i = 1; i <= 100000; i++) {
// If i is a palindrome number
if (isPalindrome(i)) {
// Stores the sum of
// the digits in i
int sum = sumOfDigits(i);
// If the sum of digits
// in i is a prime number
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
// Find the prefix sum of arr[]
for (int i = 1; i <= 100000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
// Function to count all the numbers in
// the given ranges that are palindromic
// and the sum of digits is prime numbers
void countNumbers(int Q[][2], int N)
{
// Function Call to precompute
// all the numbers till 10^5
precompute();
// Traverse the given queries Q[]
for (int i = 0; i < N; i++) {
// Print the result for
// each query
cout << (arr[Q[i][1]]
- arr[Q[i][0] - 1]);
cout << endl;
}
}
// Driver Code
int main()
{
int Q[][2] = { { 5, 9 }, { 1, 101 } };
int N = sizeof(Q) / sizeof(Q[0]);
// Function Call
countNumbers(Q, N);
}
Java
// Java program for the above approach
class GFG{
static int[] arr = new int[100005];
// Function to check if the number N
// is palindrome or not
static boolean isPalindrome(int N)
{
int temp = N;
// Store the reverse of number N
int res = 0;
// Reverse temp and store in res
while (temp != 0)
{
int rem = temp % 10;
res = res * 10 + rem;
temp /= 10;
}
// If N is the same as res, then
// return true
if (res == N)
{
return true;
}
else
{
return false;
}
}
// Function to find the sum of the
// digits of the number N
static int sumOfDigits(int N)
{
// Stores the sum of the digits
int sum = 0;
while (N != 0)
{
// Add the last digit of the
// number N to the sum
sum += N % 10;
// Remove the last digit
// from N
N /= 10;
}
// Return the resultant sum
return sum;
}
// Function to check if N is prime or not
static boolean isPrime(int n)
{
// If i is 1 or 0, then return false
if (n <= 1)
{
return false;
}
// Check if i is divisible by any
// number in the range [2, n/2]
for(int i = 2; i <= n / 2; ++i)
{
// If n is divisible by i
if (n % i == 0)
return false;
}
return true;
}
// Function to precompute all the numbers
// till 10^5 that are palindromic and
// whose sum of digits is prime numbers
static void precompute()
{
// Iterate over the range 1 to 10 ^ 5
for(int i = 1; i <= 100000; i++)
{
// If i is a palindrome number
if (isPalindrome(i))
{
// Stores the sum of
// the digits in i
int sum = sumOfDigits(i);
// If the sum of digits
// in i is a prime number
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
// Find the prefix sum of arr[]
for(int i = 1; i <= 100000; i++)
{
arr[i] = arr[i] + arr[i - 1];
}
}
// Function to count all the numbers in
// the given ranges that are palindromic
// and the sum of digits is prime numbers
static void countNumbers(int[][] Q, int N)
{
// Function Call to precompute
// all the numbers till 10^5
precompute();
// Traverse the given queries Q[]
for(int i = 0; i < N; i++)
{
// Print the result for
// each query
System.out.println((arr[Q[i][1]] -
arr[Q[i][0] - 1]));
}
}
// Driver Code
public static void main(String[] args)
{
int[][] Q = { { 5, 9 }, { 1, 101 } };
int N = Q.length;
// Function Call
countNumbers(Q, N);
}
}
// This code is contributed by user_qa7r
Python3
# Python 3 program for the above approach
arr = [0 for i in range(100005)]
# Function to check if the number N
# is palindrome or not
def isPalindrome(N):
# Store the value of N
temp = N
# Store the reverse of number N
res = 0
# Reverse temp and store in res
while (temp != 0):
rem = temp % 10
res = res * 10 + rem
temp //= 10
# If N is the same as res, then
# return true
if (res == N):
return True
else:
return False
# Function to find the sum of the
# digits of the number N
def sumOfDigits(N):
# Stores the sum of the digits
sum = 0
while (N != 0):
# Add the last digit of the
# number N to the sum
sum += N % 10
# Remove the last digit
# from N
N //= 10
# Return the resultant sum
return sum
# Function to check if N is prime or not
def isPrime(n):
# If i is 1 or 0, then return false
if (n <= 1):
return False
# Check if i is divisible by any
# number in the range [2, n/2]
for i in range(2, (n//2) + 1, 1):
# If n is divisible by i
if (n % i == 0):
return False
return True
# Function to precompute all the numbers
# till 10^5 that are palindromic and
# whose sum of digits is prime numbers
def precompute():
# Iterate over the range 1 to 10 ^ 5
for i in range(1, 100001, 1):
# If i is a palindrome number
if (isPalindrome(i)):
# Stores the sum of
# the digits in i
sum = sumOfDigits(i)
# If the sum of digits
# in i is a prime number
if (isPrime(sum)):
arr[i] = 1
else:
arr[i] = 0
else:
arr[i] = 0
# Find the prefix sum of arr[]
for i in range(1,100001,1):
arr[i] = arr[i] + arr[i - 1]
# Function to count all the numbers in
# the given ranges that are palindromic
# and the sum of digits is prime numbers
def countNumbers(Q, N):
# Function Call to precompute
# all the numbers till 10^5
precompute()
# Traverse the given queries Q[]
for i in range(N):
# Print the result for
# each query
print(arr[Q[i][1]] - arr[Q[i][0] - 1])
# Driver Code
if __name__ == '__main__':
Q = [[5, 9], [1, 101]]
N = len(Q)
# Function Call
countNumbers(Q, N)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
class GFG{
static int[] arr = new int[100005];
// Function to check if the number N
// is palindrome or not
static bool isPalindrome(int N)
{
int temp = N;
// Store the reverse of number N
int res = 0;
// Reverse temp and store in res
while (temp != 0)
{
int rem = temp % 10;
res = res * 10 + rem;
temp /= 10;
}
// If N is the same as res, then
// return true
if (res == N)
{
return true;
}
else
{
return false;
}
}
// Function to find the sum of the
// digits of the number N
static int sumOfDigits(int N)
{
// Stores the sum of the digits
int sum = 0;
while (N != 0)
{
// Add the last digit of the
// number N to the sum
sum += N % 10;
// Remove the last digit
// from N
N /= 10;
}
// Return the resultant sum
return sum;
}
// Function to check if N is prime or not
static bool isPrime(int n)
{
// If i is 1 or 0, then return false
if (n <= 1)
{
return false;
}
// Check if i is divisible by any
// number in the range [2, n/2]
for(int i = 2; i <= n / 2; ++i)
{
// If n is divisible by i
if (n % i == 0)
return false;
}
return true;
}
// Function to precompute all the numbers
// till 10^5 that are palindromic and
// whose sum of digits is prime numbers
static void precompute()
{
// Iterate over the range 1 to 10 ^ 5
for(int i = 1; i <= 100000; i++)
{
// If i is a palindrome number
if (isPalindrome(i))
{
// Stores the sum of
// the digits in i
int sum = sumOfDigits(i);
// If the sum of digits
// in i is a prime number
if (isPrime(sum))
arr[i] = 1;
else
arr[i] = 0;
}
else
arr[i] = 0;
}
// Find the prefix sum of arr[]
for(int i = 1; i <= 100000; i++)
{
arr[i] = arr[i] + arr[i - 1];
}
}
// Function to count all the numbers in
// the given ranges that are palindromic
// and the sum of digits is prime numbers
static void countNumbers(int[, ] Q, int N)
{
// Function Call to precompute
// all the numbers till 10^5
precompute();
// Traverse the given queries Q[]
for(int i = 0; i < N; i++)
{
// Print the result for
// each query
Console.WriteLine((arr[Q[i, 1]] -
arr[Q[i, 0] - 1]));
}
}
// Driver Code
static public void Main()
{
int[,] Q = { { 5, 9 }, { 1, 101 } };
int N = Q.GetLength(0);
// Function Call
countNumbers(Q, N);
}
}
// This code is contributed by Dharanendra L V.
Javascript
2
6
时间复杂度: O(N*log N)
辅助空间: O(M),其中 M 是每个查询中的最大元素。