注意事项:无。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool notpr[MAXn + 10]; int cntpr, pr[MAXn / 10 + 100];
void evapr(int n) {
notpr[1] = 1;
for (int i = 2; i <= n; ++i) {
if (notpr[i] == 0) {
pr[++cntpr] = i;
}
for (int j = 1, toppj = n / i; j <= cntpr && pr[j] <= toppj; ++j) {
notpr[i * pr[j]] = 1;
if (i % pr[j] == 0) break;
}
}
}

Luogu P3383 【模板】线性筛素数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<bits/stdc++.h>
using namespace std;
const int MAXn = 1e8;

template <typename T>
inline void read(T &a) {
char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x;
}
template <typename T, typename ...Argv>
inline void read(T &a, Argv &...argv) {
read(a), read(argv...);
}

bool notpr[MAXn + 10]; int cntpr, pr[MAXn / 10 + 100];
void evapr(int n) {
notpr[1] = 1;
for (int i = 2; i <= n; ++i) {
if (notpr[i] == 0) {
pr[++cntpr] = i;
}
for (int j = 1, toppj = n / i; j <= cntpr && pr[j] <= toppj; ++j) {
notpr[i * pr[j]] = 1;
if (i % pr[j] == 0) break;
}
}
}

int n, m;
signed main() {
read(n, m);
evapr(n);
for (int i = 1, x; i <= m; ++i) {
read(x);
printf("%d\n", pr[x]);
}
return 0;
}