欧拉筛模板

· · 算法·理论

已 AC P3383【模板】线性筛素数 & P5723【深基4.例13】质数口袋

#include<iostream>
using namespace std;
const int N = 1e8 + 7;
bool noP[N];
int ct, pr[N];
void euler(int n){
    noP[0] = noP[1] = true;
    for(int i = 2; i < N; i++){
        if(!noP[i]) pr[++ct] = i;
        for(int j = 1; j <= ct and (long long) i * pr[j] < N; ++j){
            noP[i * pr[j]] = true;
            if(i % pr[j] == 0) break;
        }
    }
} 
int main(){
    std::ios::sync_with_stdio(0);
    int n, q;
    cin>>n>>q;
    euler(n);
    for(int i = 0 ; i < q; i++){
        int t;
        cin>>t;
        cout<<pr[t]<<endl;
    }
}