UVA1210
在这里插入图片描述

筛出素数,处理成前缀和,$O(n^2)$枚举每一种和的可能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 10000;
bool IsPrime[MAXN + 1];
vector<int> PTable;
int sum[1230];

void InitPTable() {
for (int i = 2; i <= MAXN; ++i) {
if (IsPrime[i] == false) {
PTable.push_back(i);
for (int j = 2 * i; j <= MAXN; j += i) {
IsPrime[j] = true;
}
}
}
}

void InitPresum() {
sum[1] = PTable[0];
for (int i = 2; i <= 1229; ++i) {
sum[i] = sum[i - 1] + PTable[i - 1];
}
}

int main() {
InitPTable();
InitPresum();
int n;
while (cin >> n && n) {
int Ans = 0;
for (int i =1; i < 1229; ++i) {
for (int j = 0; j < i; ++j) {
Ans += (sum[i] - sum[j] == n);
}
}
printf("%d\n", Ans);
}
return 0;

}