重生之苦钻算法 题解分享 · 2025/3/15
线性素数筛 - 题解
``` #include <bits/stdc++.h> using namespace std; using ll = long long; vector<ll> primes; vector<bool> isPrime; void Oula(ll n) { isPrime.resize(n+1, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= n; ++i) { if (isPrime[i]) primes.push_back(i); for (int j = 0; j < primes.size() && i * primes[j] <= n; ++j) { isPrime[i*primes[j]] = false; if (i % primes[j] == 0) break; } } } int main() { ios::sync_with_stdio(0), cin.tie(0); ll n, q; cin >> n >> q; Oula(n); while (q--) { int k; cin >> k; cout << primes[k-1] << '\n'; } return 0; } ```
查看全文
0 0 0 2