线性筛素数

#include<bits/stdc++.h>
using namespace std;
template<typename T>
void read(T& x) {
	x=0;
	int f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
		x=10*x+ch-'0',ch=getchar();
	x=x*f;
}
template<typename T>
void write(T x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
int n,q;
bool notp[100000005];
int prime[1000005],cntp;
void work(int n) {
	notp[1]=1;
	for(int i=2; i<=n; i++) {
		if(!notp[i])prime[++cntp]=i;
		for(int j=1; j<=cntp; j++) {
			if(prime[j]*i>n)break;
			notp[prime[j]*i]=1;
			if(i%prime[j]==0||prime[j]%i==0)break;
		}
	}
}
signed main() {
	ios::sync_with_stdio(false);
	read(n),read(q);
	work(n);
	while(q--) {
		int k;
		read(k);
		write(prime[k]);
		putchar('\n');
	}
	return 0;
}
posted @ 2023-06-09 18:29  N0zoM1z0  阅读(4)  评论(0编辑  收藏  举报