可以用暴力法写的,用尺取法写了,模型就是连续区间和,然后罗勇军的代码有点问题,需要稍加改动,
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int cnt;
int a[10005];
bool isprime(int x) {
if (x <= 1) return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return false;
}
return true;
}
void init() {
for (int i = 2; i <= 10000; i++) {
if (isprime(i)) a[cnt++] = i;
}
}
int main() {
init();
int n;
while (cin >> n && n) {
int l = 0, r = 0;
int sum = a[0];
int ans = 0;
while (r < cnt) {
if (sum >= n) {
if (sum == n) ans++;
sum -= a[l++];
if (l > r && l < cnt) { r = l; sum = a[l]; }
} else {
if (++r < cnt) sum += a[r];
}
}
cout << ans << endl;
}
return 0;
}