欧拉筛,本地过了,全RE

P3383 【模板】线性筛素数

shaun2000 @ 2024-08-01 14:21:10


#include <iostream>

using namespace std;

bool book[100000005];
int p[100000005];int n,k,q,cnt=0;

void op()
{
    for(int i=2;i<=n;i++)
        book[i]=1;

    for(int i=2;i<=n;i++)
    {
        if(book[i])
        {
            p[++cnt]=i;
            for(int j=1;j<=cnt;j++)
                book[p[j]*i]=0;
        }
        else
        {
            for(int j=1;j<=cnt;j++)
            {
                book[p[j]*i]=0;
                if(i%p[j]==0)
                    break;
            }
        }
    }

}

int main()
{

    scanf("%d%d",&n,&q);
    op();
    for(;q>0;q--)
    {
        scanf("%d",&k);
        printf("%d\n",p[k]);
    }

    return 0;
}

by shaun2000 @ 2024-08-01 14:22:41

之前埃氏筛过了,改成了欧拉筛,就全RE了


by meifan666 @ 2024-08-01 14:24:44

@shaun2000 int数组开小点试试,可能炸了


by ltl0825 @ 2024-08-01 14:24:50

加上i*p[j]<=n试一下


by ltl0825 @ 2024-08-01 14:25:16

@shaun2000


by ltl0825 @ 2024-08-01 14:25:35

枚举j那里


by shaun2000 @ 2024-08-01 14:49:08

@ltl0825 过了,谢谢


|