'commit'
This commit is contained in:
26
TangDou/AcWing/Prime/868_2.cpp
Normal file
26
TangDou/AcWing/Prime/868_2.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 欧拉筛
|
||||
const int N = 1e6 + 10;
|
||||
int primes[N], cnt; // primes[]存储所有素数
|
||||
int st[N]; // st[x]存储x是否被筛掉
|
||||
|
||||
void get_primes(int n) {
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (!st[i]) primes[cnt++] = i;
|
||||
for (int j = 0; primes[j] * i <= n; j++) { // 捋着质数数组来
|
||||
st[primes[j] * i] = 1; // 质数因子的i倍被干掉
|
||||
if (i % primes[j] == 0) break; // 只被它的最小质因子筛选一次
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
get_primes(n);
|
||||
printf("%d\n", cnt);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user