luogu:https://www.luogu.com.cn/problem/P3383
acwing:https://www.acwing.com/problem/content/870/

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, M = 1e8 + 10;
bool st[M];
int prime[N], cnt, n, q, k;
void sieve(int n){
	st[1] = true;
	for (int i = 2; i <= n; i ++ ){
		if (!st[i]) prime[ ++ cnt] = i;
		for (int j = 1; j <= cnt && i * prime[j] <= n; j++){
			st[i * prime[j]] = true;
			if (i % prime[j] == 0) break;
		}
	}
}
int main(){
	cin >> n >> q;  //质数范围为n
	sieve(n);
	while (q--){  //q次询问,查询第k个质数
		scanf("%d", &k);
		cout << prime[k] << "\n";
	}
	return 0;
}
posted on 2021-10-13 13:15  Hamine  阅读(29)  评论(0编辑  收藏  举报