给定大小为Q的两个数组L []和R [] ,任务是从[L [i],R [i]范围中找到复合幻数的数量,即既是复合数又是幻数的数字] (0≤i 例子: Input: Q = 1, L[] = {10}, R[] = {100} Input: Q = 1, L[] = {1200}, R[] = {1300} 天真的方法:解决问题的最简单方法是遍历[L [i],R [i]] (0≤i 时间复杂度: O(Q * N 3/2 ) 高效方法:要优化上述方法,请在一定范围内预先计算并存储组合幻数的计数。这样可以将每个查询的计算复杂度优化为O(1)。请按照以下步骤解决问题: 下面是上述方法的实现: 时间复杂度: O(N 3/2 )
Output: 8
Explanation: Numbers in the range [L[0], R[0]] which are both composite as well as Magic numbers are {10, 28, 46, 55, 64, 82, 91, 100}.
Output: 9
Explanation: Numbers in the range [L[0], R[0]] which are both composite as well as Magic numbers are {1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288}.
辅助空间: O(N)
C++
// C++ program to implement
// the above approach
#include
Java
// Java program to implement
// the above approach
class GFG
{
// Check if a number is magic
// number or not
static boolean isMagic(int num)
{
return (num % 9 == 1);
}
// Check number is composite
// number or not
static boolean isComposite(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return false;
// Check if the number is
// a multiple of 2 or 3
if (n % 2 == 0 || n % 3 == 0)
return true;
// Check for multiples of remaining primes
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true;
return false;
}
// Function to find Composite Magic
// Numbers in given ranges
static void find(int L[], int R[], int q)
{
// dp[i]: Stores the count of
// composite Magic numbers up to i
int dp[] = new int[1000005];
dp[0] = 0;
dp[1] = 0;
// Traverse in the range [1, 1e5)
for (int i = 1; i < 1000005; i++)
{
// Check if number is Composite number
// as well as a Magic number or not
if (isComposite(i) && isMagic(i) == true)
{
dp[i] = dp[i - 1] + 1;
}
else
dp[i] = dp[i - 1];
}
// Print results for each query
for (int i = 0; i < q; i++)
System.out.println(dp[R[i]] - dp[L[i] - 1]);
}
// Driver Code
public static void main (String[] args)
{
int L[] = { 10, 3 };
int R[] = { 100, 2279 };
int Q = 2;
find(L, R, Q);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program to implement
# the above approach
# Check if a number is magic
# number or not
def isMagic(num):
return (num % 9 == 1)
# Check number is composite
# number or not
def isComposite(n):
# Corner cases
if (n <= 1):
return False
if (n <= 3):
return False
# Check if the number is
# a multiple of 2 or 3
if (n % 2 == 0 or n % 3 == 0):
return True
# Check for multiples of remaining primes
for i in range(5, n + 1, 6):
if i * i > n + 1:
break
if (n % i == 0 or n % (i + 2) == 0):
return True
return False
# Function to find Composite Magic
# Numbers in given ranges
def find(L, R, q):
# dp[i]: Stores the count of
# composite Magic numbers up to i
dp = [0] * 1000005
dp[0] = 0
dp[1] = 0
# Traverse in the range [1, 1e5)
for i in range(1, 1000005):
# Check if number is Composite number
# as well as a Magic number or not
if (isComposite(i) and isMagic(i)):
dp[i] = dp[i - 1] + 1
else:
dp[i] = dp[i - 1]
# Print results for each query
for i in range(q):
print(dp[R[i]] - dp[L[i] - 1])
# Driver Code
if __name__ == '__main__':
L = [ 10, 3 ]
R = [ 100, 2279 ]
Q = 2
find(L, R, Q)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Check if a number is magic
// number or not
static bool isMagic(int num)
{
return (num % 9 == 1);
}
// Check number is composite
// number or not
static bool isComposite(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return false;
// Check if the number is
// a multiple of 2 or 3
if (n % 2 == 0 || n % 3 == 0)
return true;
// Check for multiples of remaining primes
for(int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true;
return false;
}
// Function to find Composite Magic
// Numbers in given ranges
static void find(int[] L, int[] R, int q)
{
// dp[i]: Stores the count of
// composite Magic numbers up to i
int[] dp = new int[1000005];
dp[0] = 0;
dp[1] = 0;
// Traverse in the range [1, 1e5)
for(int i = 1; i < 1000005; i++)
{
// Check if number is Composite number
// as well as a Magic number or not
if (isComposite(i) && isMagic(i) == true)
{
dp[i] = dp[i - 1] + 1;
}
else
dp[i] = dp[i - 1];
}
// Print results for each query
for(int i = 0; i < q; i++)
Console.WriteLine(dp[R[i]] - dp[L[i] - 1]);
}
// Driver Code
public static void Main ()
{
int[] L = { 10, 3 };
int[] R = { 100, 2279 };
int Q = 2;
find(L, R, Q);
}
}
// This code is contributed by susmitakundugoaldanga
8
198
辅助空间: O(N )