You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
687 B
28 lines
687 B
#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;
|
|
} |