能指导指导吗?

P3383 【模板】线性筛素数

qianhy_cool @ 2024-10-26 15:35:57

rt

#include <iostream>
bool mp[100000000];
int a[100000000];
int n, q, k;
using namespace std;
void prime(int num) {
    mp[1] = true;
    int sum = 1;
    for (int i = 2; i <= num; ++i) {
        if (!mp[i]) {
            a[sum] = i;
            ++sum;
            for (int j = i * 2; j <= num; j += i) {
                mp[j] = true;
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin >> n >> q;
    prime(n);
    for (int i = 1; i <= q; ++i) {
        cin >> k;
        cout << a[k] << "\n";
    }
}

by huyichen0903 @ 2024-10-27 13:36:33

@qianhy_cool 你这不是线性筛……


by qianhy_cool @ 2024-10-27 17:27:02

@huyichen0903 那怎么弄


by qianhy_cool @ 2024-10-27 17:27:45

不会......


by huyichen0903 @ 2024-10-27 18:36:50

@qianhy_cool

#include<bits/stdc++.h>
using namespace std;
int n,l,c[1000001];
bool b[1000001];
int main(){
    scanf("%d",&n),c[0]=1e9;
    for (int i=2;i<=n;i++){
        if (!b[i]) c[++l]=i,printf("%d ",i);
        for (int j=1;j<=l&&i*c[j]<=n&&i%c[j-1];j++) b[i*c[j]]=true;
    }
    return 0;
}

你研究一下


by qianhy_cool @ 2024-10-27 18:47:21

@huyichen0903 您码风是真的偏那


by huyichen0903 @ 2024-10-27 18:56:01

@qianhy_cool jbl


|