建议加强数据,埃氏筛轻松水过?!
查看原帖
建议加强数据,埃氏筛轻松水过?!
1074084
asd890123楼主2024/4/23 08:53
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>

const int maxn = 1e8 + 5;

int main(){
    
    int n,q,k;
    bool a[maxn];
    std::vector <int> p(1);
    
    scanf("%d%d",&n,&q);
    memset(a,1,sizeof(bool) * (n + 1));
    a[0] = a[1] = 0;
    for (int i = 2;i * i <= n;i++)
        if (a[i])
            for (int j = i * 2;j <= n;j += i) 
                a[j] = 0;
    for (int i = 1;i <= n;i++) 
        if (a[i])
            p.push_back(i);
    while (q--) scanf("%d",&k),printf("%d\n",p[k]);
    
    return 0;
}

如果有人没过,怀疑我是骗子,先看眼,是不是把外层循环条件写成i<=n,应该是i*i<=n,但这仍然不应该通过,所以建议增强数据(目前最长1.46s)!

2024/4/23 08:53
加载中...