一片紫(RE)

P3383 【模板】线性筛素数

LG_Sam @ 2024-08-15 15:29:13

#include<bits/stdc++.h>
using namespace std;
int v[33550336],prime[33550336],c=0;
void make_prime(int n)
{
    int i,j;
    for(i=2;i<=n;i++){
        if(v[i]==false){
            c++;
            prime[c]=i;
        }
        for(j=1;(j<=c)&&(i*prime[j]<=n);j++){
            v[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }
}
int m;
int main(){
    cin.tie(0);
    cout.tie(0);
    cin>>m;
    make_prime(m);
    for(int i=1;i<=c;i++) cout<<prime[i]<<endl;
    return 0;
}

by T_TwY @ 2024-08-15 15:37:34

哥们你好像没读入完吧


by _hJL_ @ 2024-08-15 15:38:21

直译:一片#9D3DCF(RE)


by T_TwY @ 2024-08-15 15:41:31

还有v数组开小了,要开1e8

#include<bits/stdc++.h>
using namespace std;
const int N=1e8+10;
int v[N],prime[33550336],c=0;
int q,k;
void make_prime(int n)
{
    int i,j;
    for(i=2;i<=n;i++){
        if(v[i]==false){
            c++;
            prime[c]=i;
        }
        for(j=1;(j<=c)&&(i*prime[j]<=n);j++){
            v[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }
}
int m;
int main(){
    cin.tie(0);
    cout.tie(0);
    cin>>m;
    cin>>q;
    make_prime(m);
    while(q--)
    {
        cin>>k;
        printf("%d\n",prime[k]);
    }
    return 0;
}

by T_TwY @ 2024-08-15 15:43:04

@LG_Sam


by LG_Sam @ 2024-08-15 16:49:39

@T_TwY 6


|