Luogu_P3383 [Template] Linear Sieve Primes_Euclidean Sieve

 

 

//
#include<bits/stdc++.h>
using namespace std;

const int N=1e8+9;
int prime[N];
bool judge[N];

void sieve()
{
    memset( prime,0,sizeof( prime ) );
    memset( judge,0,sizeof( judge ) );

    int i,j,cnt=0;
    judge[1]=1;			// judge 也可以用来预判断 prime 

    for( i=2;i<N;i++ )
    {
        if( judge[i]==0 ) prime[cnt++]=i;

        for( j=0;j<cnt && i<=N/prime[j];j++ )
        {
            judge[ i*prime[j] ]=1; 
            if( i%prime[j]==0 ) break;
        }
    }
}

int main()
{
    sieve(); int n,q,x;

    while( ~scanf("%d%d",&n,&q) )
    {
        while( q-- )
        {
            scanf("%d",&x);
            printf("%d\n",prime[x-1]);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_63173957/article/details/123933893