为什么总是RE啊,不能理解

P3383 【模板】线性筛素数

fw20245242 @ 2024-12-24 21:34:37

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e8 + 10;
long long int a[N],cnt;
bool st[N];

void get_prime(long long int x)
{
    for (int i = 2;i <= x;i++)
    {
        if (!st[i]) a[cnt++] = i;
        for (int j = 0;a[j] * i <= x;j++)
        {
            st[a[j] * i] = true;
            if (i % a[j] == 0) break;
        }
    }
}
int main()
{
    long long int n, m;
    cin >> n>>m;

    get_prime(n);
    for (int i = 0;i <m;i++)
    {
        long long int t;
        cin >> t;
        cout << a[t - 1] << endl;
    }
    return 0;
}

by wangyubo1227 @ 2024-12-24 21:37:29

fill(st, st + N, false);@fw20245242


by wangyubo1227 @ 2024-12-24 21:37:50

st数组没初始化


by guoxinchen @ 2024-12-25 08:01:57

@fw20245242你没写j<cnt,不然a[j]=0时i%0直接炸了


|