📜  对于 Q 个查询,L 到 R 范围内的第 K 个最小素数

📅  最后修改于: 2022-05-13 01:56:08.913000             🧑  作者: Mango

对于 Q 个查询,L 到 R 范围内的第 K 个最小素数

给定三个变量L、RQ ,它们表示范围 [L, R] 和查询总数。对于每个查询,都会有一个变量K 。任务是在[L, R]范围内找到第 K个最小的素数。如果K大于[L, R]范围内的素数计数,则返回-1

例子:

方法:给定的问题可以在埃拉托色尼筛法的帮助下解决:
当算法终止时,列表中所有未标记的数字都是素数。请按照以下步骤操作:

  • 对 [L, R] 范围内的数字运行Eratosthenes 筛法
  • 对于每个查询,从计算的素数中找到第 K 个最小的素数。
  • 如果 K 超过该范围内的素数数,则返回 -1。

请参阅下图以更好地了解埃拉托色尼筛法。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
vector primes;
 
// Function to implement
// Sieve Of Eratosthenes
void sieveOfEratosthenes(int R)
{
    primes.assign(R + 1, 0);
    primes[1] = 1;
    for (int i = 2; i * i <= R; i++) {
        if (primes[i] == 0) {
            for (int j = i + i; j <= R;
                 j += i)
                primes[j] = 1;
        }
    }
}
 
// Function to return Kth smallest
// prime number if it exists
int kthSmallest(int L, int R, int K)
{
    // To count the prime number
    int count = 0;
 
    // Loop to iterate the from L to R
    for (int i = L; i <= R; i++) {
 
        // Calculate the count Of
        // primes numbers
        if (primes[i] == 0) {
            count++;
        }
 
        // if count equals K, then
        // Kth smallest prime number is
        // found then return the number
        if (count == K) {
            return i;
        }
    }
 
    // Kth smallest prime number is not in
    // this range then return -1
    return -1;
}
 
// Driver Code
int main()
{
    // No of Queries
    int Q = 3;
    int L, R, K;
    L = 5, R = 20;
    sieveOfEratosthenes(R);
 
    // First Query
    K = 4;
    cout << kthSmallest(L, R, K) << endl;
 
    // Second Query
    K = 3;
    cout << kthSmallest(L, R, K) << endl;
 
    // Third Query
    K = 5;
    cout << kthSmallest(L, R, K) << endl;
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
 
  static int primes[] = new int[1000];
 
  // Function to implement
  // Sieve Of Eratosthenes
  static void sieveOfEratosthenes(int R)
  {
 
    for(int i = 0; i < R + 1; i++) {
      primes[i] = 0;
    }
 
    primes[1] = 1;
    for (int i = 2; i * i <= R; i++) {
      if (primes[i] == 0) {
        for (int j = i + i; j <= R;
             j += i)
          primes[j] = 1;
      }
    }
  }
 
  // Function to return Kth smallest
  // prime number if it exists
  static int kthSmallest(int L, int R, int K)
  {
    // To count the prime number
    int count = 0;
 
    // Loop to iterate the from L to R
    for (int i = L; i <= R; i++) {
 
      // Calculate the count Of
      // primes numbers
      if (primes[i] == 0) {
        count++;
      }
 
      // if count equals K, then
      // Kth smallest prime number is
      // found then return the number
      if (count == K) {
        return i;
      }
    }
 
    // Kth smallest prime number is not in
    // this range then return -1
    return -1;
  }
 
  // Driver code
  public static void main(String args[])
  {
    // No of Queries
    int Q = 3;
    int L = 5, R = 20;
    sieveOfEratosthenes(R);
 
    // First Query
    int K = 4;
    System.out.println(kthSmallest(L, R, K));
 
    // Second Query
    K = 3;
    System.out.println(kthSmallest(L, R, K));
 
    // Third Query
    K = 5;
    System.out.println(kthSmallest(L, R, K));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code to implement the above approach
primes = [0 for i in range(1000)];
 
# Function to implement
# Sieve Of Eratosthenes
def sieveOfEratosthenes(R):
    for i in range(0, R + 1):
        primes[i] = 0;
 
    primes[1] = 1;
    for i in range(2, pow(R, 2) + 1):
        if (primes[i] == 0):
            for j in range(i + i, R + 1, i):
                primes[j] = 1;
 
# Function to return Kth smallest
# prime number if it exists
def kthSmallest(L, R, K):
   
    # To count the prime number
    count = 0;
 
    # Loop to iterate the from L to R
    for i in range(L,R+1):
 
        # Calculate the count Of
        # primes numbers
        if (primes[i] == 0):
            count += 1;
 
        # if count equals K, then
        # Kth smallest prime number is
        # found then return the number
        if (count == K):
            return i;
 
    # Kth smallest prime number is not in
    # this range then return -1
    return -1;
 
# Driver code
if __name__ == '__main__':
    # No of Queries
    Q = 3;
    L = 5; R = 20;
    sieveOfEratosthenes(R);
 
    # First Query
    K = 4;
    print(kthSmallest(L, R, K));
 
    # Second Query
    K = 3;
    print(kthSmallest(L, R, K));
 
    # Third Query
    K = 5;
    print(kthSmallest(L, R, K));
 
# This code is contributed by shikhasingrajput


C#
// C# code to implement the above approach
using System;
class GFG
{
 
  static int []primes = new int[1000];
 
  // Function to implement
  // Sieve Of Eratosthenes
  static void sieveOfEratosthenes(int R)
  {
 
    for(int i = 0; i < R + 1; i++) {
      primes[i] = 0;
    }
 
    primes[1] = 1;
    for (int i = 2; i * i <= R; i++) {
      if (primes[i] == 0) {
        for (int j = i + i; j <= R;
             j += i)
          primes[j] = 1;
      }
    }
  }
 
  // Function to return Kth smallest
  // prime number if it exists
  static int kthSmallest(int L, int R, int K)
  {
    // To count the prime number
    int count = 0;
 
    // Loop to iterate the from L to R
    for (int i = L; i <= R; i++) {
 
      // Calculate the count Of
      // primes numbers
      if (primes[i] == 0) {
        count++;
      }
 
      // if count equals K, then
      // Kth smallest prime number is
      // found then return the number
      if (count == K) {
        return i;
      }
    }
 
    // Kth smallest prime number is not in
    // this range then return -1
    return -1;
  }
 
  // Driver code
  public static void Main()
  {
    // No of Queries
    int Q = 3;
    int L = 5, R = 20;
    sieveOfEratosthenes(R);
 
    // First Query
    int K = 4;
    Console.WriteLine(kthSmallest(L, R, K));
 
    // Second Query
    K = 3;
    Console.WriteLine(kthSmallest(L, R, K));
 
    // Third Query
    K = 5;
    Console.WriteLine(kthSmallest(L, R, K));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
13
11
17

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