埃氏筛怎么改才能过

P3383 【模板】线性筛素数

Happiness_3 @ 2024-11-05 12:51:33

#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 1e8 + 10;
ll n,q,vis[N],prime[N];

void sieve() {
    for(ll i = 4 ; i <= n ; i += 2) {
        vis[i] = 1;
    }
    prime[1] = 2;
    ll k = 2;
    for(ll i = 3 ; i <= n  ; i += 2) {
        if(vis[i] == 0) {
            prime[k++] = i;
            for(ll j = i ; j <= n / i ; ++j) {
                vis[i*j] = 1;
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> n >> q;
    sieve();
    while(q--) {
        ll x;
        cin >> x;
        cout << prime[x] << endl;
    }
    return 0;
}

by HEPwP @ 2024-11-05 12:56:32

@Happiness_3 你可能永远也过不了


by masonxiong @ 2024-11-05 13:26:53

@HEPwP 这题埃氏筛是能过的哦


by BLX32M_10 @ 2024-11-05 13:27:59

@Happiness_3 vis 改成 bitset


by masonxiong @ 2024-11-05 13:29:49

@Br00k5xx 正确的。


by Happiness_3 @ 2024-11-05 16:42:08

@Br00k5xx 谢谢!!!


by Happiness_3 @ 2024-11-05 16:42:24

@masonxiong 谢谢!!!


|