luogu#P3383. 【模板】线性筛素数

【模板】线性筛素数

Background

This problem has been updated: it now asks for querying the kk-th smallest prime instead of primality testing.

Hint: The input/output and computation workload can be large.

  • For C++: if you use cin for I/O, consider std::ios::sync_with_stdio(0) for acceleration, and use '\n' for newlines.
  • For Java: using a linear sieve with optimized I/O can pass within the time limit, though it may be tight.
  • For Python: performance varies widely by implementation; using arrays from the numpy library instead of lists, together with the Sieve of Eratosthenes, can still pass within reasonable time and memory limits.

Problem Description

Given an upper bound nn and qq queries, for each query output the kk-th smallest prime number.

Input Format

The first line contains two positive integers n,qn, q, representing the query range and the number of queries.

Each of the next qq lines contains a positive integer kk, asking for the kk-th smallest prime.

Output Format

Output qq lines, each containing a single integer as the answer.

100 5
1
2
3
4
5
2
3
5
7
11

Hint

Constraints
For 100%100\% of the testdata, n=108n = 10^8, 1q1061 \le q \le 10^6, and it is guaranteed that the requested primes do not exceed nn.

Data by NaCly_Fish.

Translated by ChatGPT 5