用欧拉筛交了n次,一直RE

P3383 【模板】线性筛素数

Wyl20110306 @ 2024-07-18 20:03:01


#include<bits/stdc++.h>
using namespace std;
const int N=100000001;
int n; 
bool flag[N];
int pri[N],pn;
int main(){
    cin>>n;
    for(int i=2;i<=n;i++){
        if(flag[i]==0) pri[++pn]=i;
        for(int j=1;j<=pn&&j*pri[i]<=n;j++){
            flag[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
    for(int i=1;i<=pn;i++) cout<<pri[i]<<endl;
    return 0;
}

by pengzihao @ 2024-07-18 20:05:59

第二个循环条件反了


by PengDave @ 2024-07-18 20:07:52

@Wyl20110306 是i*pri[j]<=n


by Wyl20110306 @ 2024-07-18 20:09:11

@PengDave 好的,我试下


by Wyl20110306 @ 2024-07-18 20:10:55

@PengDave 还是RE


by PengDave @ 2024-07-18 20:12:40

@Wyl20110306 再说一遍,是i*pri[j]而不是j*pri[j]j*pri[i]


by xuhaotian @ 2024-07-18 20:12:41

@Wyl20110306 你确定你看题了?


by xuhaotian @ 2024-07-18 20:13:08

求的是第 k 小质数


by PengDave @ 2024-07-18 20:13:47

@Wyl20110306 再说,题目要你输出什么你都搞错了


by xuhaotian @ 2024-07-18 20:14:32

#include<bits/stdc++.h>
using namespace std;
const int N=100000001;
int n,m; 
bool flag[N];
int pri[N],pn;
int main(){
    cin>>n>>m;
    for(int i=2;i<=n;i++){
        if(flag[i]==0) pri[++pn]=i;
        for(int j=1;j<=pn&&i*pri[j]<=n;j++){
            flag[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
    for(int i=1;i<=m;i++){
        int k;
        cin>>k;
        cout<<pri[k]<<endl;
    }
    return 0;
}

by Wyl20110306 @ 2024-07-18 20:18:20

@PengDave 哦,我知道了


| 下一页