📜  查询给定范围[L,R]中的组合幻数

📅  最后修改于: 2021-04-22 06:10:25             🧑  作者: Mango

给定大小为Q的两个数组L []R [] ,任务是从[L [i],R [i]范围中找到复合幻数的数量,即既是复合数又是幻数的数字] (0≤i

例子:

天真的方法:解决问题的最简单方法是遍历[L [i],R [i]] (0≤i

时间复杂度: O(Q * N 3/2 )
辅助空间: O(N)

高效方法:要优化上述方法,请在一定范围内预先计算并存储组合幻数的计数。这样可以将每个查询的计算复杂度优化为O(1)。请按照以下步骤解决问题:

  1. 初始化一个整数数组dp [] ,使dp [i]存储最多i复合幻数的计数。
  2. 遍历范围[ 1,10 6 ] ,对于该范围内的每个数字,请检查其是否为合成幻数。如果发现是真的,则用dp [i – 1] +1更新dp [i ]。否则,用dp [i – 1]更新dp [i ]。
  3. 同时遍历数组L []R []并打印dp [R [i]] – dp [L [i] – 1]作为所需答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Check if a number is magic
// number or not
bool isMagic(int num)
{
    return (num % 9 == 1);
}
 
// Check number is composite
// number or not
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
void find(int L[], int R[], int q)
{
    // dp[i]: Stores the count of
    // composite Magic numbers up to i
    int dp[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)) {
            dp[i] = dp[i - 1] + 1;
        }
 
        else
            dp[i] = dp[i - 1];
    }
 
    // Print results for each query
    for (int i = 0; i < q; i++)
        cout << dp[R[i]] - dp[L[i] - 1] << endl;
}
 
// Driver Code
int main()
{
    int L[] = { 10, 3 };
    int R[] = { 100, 2279 };
    int Q = 2;
 
    find(L, R, Q);
    return 0;
}


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 3/2 )
辅助空间: O(N )