感觉最常用线性筛的时候就是哈希找素数了2333
时间复杂度:O(n)
1 #include<set> 2 #include<cmath> 3 #include<ctime> 4 #include<queue> 5 #include<stack> 6 #include<cstdio> 7 #include<vector> 8 #include<cstring> 9 #include<cstdlib> 10 #include<iostream> 11 #include<algorithm> 12 #define N 100000001 13 using namespace std; 14 int p[N],n,tot;bool b[N]; 15 inline void prime(int n){ 16 for(int i=2;i<=n;i++){ 17 if(!b[i]) p[++tot]=i; 18 for(int j=1;j<=tot&&i*p[j]<=n;j++){ 19 b[i*p[j]]=true; 20 if(!(i%p[j])) break; 21 } 22 } 23 } 24 inline void init(){ 25 scanf("%d",&n); 26 prime(n); 27 } 28 int main(){ 29 freopen("prime.in","r",stdin); 30 freopen("prime.out","w",stdout); 31 init(); 32 fclose(stdin); 33 fclose(stdout); 34 return 0; 35 }