Files
python/TangDou/LuoGuBook/P1955_Prepare.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
893 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 10000810;
int primes[N], cnt;
bool st[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i; //没有被标识过,就增加到质数数组中去
// 线性筛法核心:N 只会被它最小的质因子筛掉
// 从小到大枚举所有可能满足条件的质数
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true; //从小向大所以这里可以这么写primes[j],标识st[primes[j] * i]已经标识过了
//最小质因子就可以了,其它质因子放弃掉,看上面的链接里有原因说明
if (i % primes[j] == 0) break;
}
}
}
int main() {
get_primes( 1e6 + 10);
for (int i = 0; i < cnt; i++) cout << primes[i] << " ";
return 0;
}