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

28 lines
687 B
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
// 线性筛法求素数 (简称 欧拉筛)
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;
for (int j = 0; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
int L, ans, sum;
int main() {
cin >> L;
get_primes(L);
for (int i = 0; i < cnt; i++) {
if (ans + primes[i] > L) break;
ans += primes[i];
printf("%d\n", primes[i]);
sum++;
}
printf("%d\n", sum);
return 0;
}