【模板】线性筛素数

线性的素数筛……

证明过程见这篇文章

上代码

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10000005
using namespace std;
int n,m,primenum,asknum;
int primelist[MAXN],isnotprime[MAXN];

inline void getnum(int &num)
{
num=0;
char c=getchar();
while (!isdigit(c))
c=getchar();
while (isdigit(c))
{
num=(num<<3)+(num<<1)+(c&15);
c=getchar();
}
return;
}

inline void getprime(int x)
{
memset(primelist,0,sizeof(primelist));
memset(isnotprime,0,sizeof(isnotprime));
primenum=0;
isnotprime[0]=1,isnotprime[1]=1;
for (register int i=2;i<=x;++i)
{
if (!isnotprime[i])
primelist[++primenum]=i;
for (register int j=1;j<=primenum&&i*primelist[j]<=x;++j)
{
isnotprime[i*primelist[j]]=1;
if (!(i%primelist[j]))
break;
}
}
return;
}

int main()
{
getnum(n),getnum(m);
getprime(n);
while (m--)
{
getnum(asknum);
printf("%s\n",isnotprime[asknum]?"No":"Yes");
}
return 0;
}
-------------本文结束,感谢阅读-------------
0%