(板子)线性筛素数 LuoguP3383
Posted at 17-8-22 09:23, Updated at 19-10-16 21:59
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int Maxn = 10000000+100; int IsNotPrime[Maxn], Prime[Maxn]; int N, M, Prime_Num; inline void work () { for (int i = 2; i <= N; ++ i) { if (!IsNotPrime[i])Prime[++Prime_Num] = i; for (int j = 1; j <= Prime_Num && (i * Prime[j] <= N); ++ j) { IsNotPrime[i * Prime[j]] = 1; if (!(i % Prime[j])) break; } } } int main() { #ifndef ONLINE_JUDGE freopen("Prime.in", "r", stdin); freopen("Prime.out", "w", stdout); #endif scanf("%d %d", &N, &M); IsNotPrime[0] = 1; IsNotPrime[1] = 1; work(); int x; for (int i = 1; i <= M; ++ i) { scanf("%d", &x); if(!IsNotPrime[x]) puts("Yes"); else puts("No"); } return 0; }
|