素数筛法

· · 个人记录

#include <cstdio>
#define N 10000005
using namespace std;

int n, m;
bool shus[N];

void shai(int x) {
    shus[1] = true;
    int sel = x / 2 + 2;
    for(int i = 2;i < sel;i++)
        if(!shus[i])
            for(int j = i * 2;j <= x;j += i)
                shus[j] = true;
}

void init() {
    scanf("%d%d", &n, &m);
    shai(n);
}

void work() {
    int x;
    for(int i = 0;i < m;i++) {
        scanf("%d", &x);
        shus[x] ? puts("No") : puts("Yes");
    }
}

int main() {
    init();
    work();
    return 0;
}