线性(欧拉)筛法模板
题目:https://www.luogu.com.cn/problem/P3383
vector<int> prime;
for(int i=2;i<=n;++i){
if(vis[i]==0)
prime.push_back(i);
for(int j=0;i*prime[j]<=n;++j){
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
完整代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 1e8+5;
int n, k;
int vis[M];
int main(){
scanf("%d %d",&n,&k);
vector<int> prime;
for(int i=2;i<=n;++i){
if(vis[i]==0)
prime.push_back(i);
for(int j=0;i*prime[j]<=n;++j){
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
while(k--){
int num;
scanf("%d",&num);
printf("%d\n",prime[num-1]);
}
}