http://poj.org/problem?id=2739
普通的查找连续子段和相等的问题,素数打表+尺取法,我使用的是左闭右开区间。水题
/*AC*/
/* Memory 148K Time 0MS*/
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
bool isPrime[10000+1];
int prime[1500]; //可以用高斯公式x/ln(x)来大致估算10000以内的素数有多少个,当然了,1229个,当做常识吧
int now;
void table(){
memset(isPrime, 1, sizeof(isPrime));
now = 0;
for(int i=2; i<=100; i++){
if(isPrime[i]){
prime[now++] = i;
for(int j=2*i; j<=10000; j+=i){
isPrime[j] = 0;
}
}
}
for(int i=101; i<=10000; i++){
if(isPrime[i])
prime[now++] = i;
}
}
int main(){
int n;
table();
//printf("%d\n", now);
while(scanf("%d", &n) != EOF){
if(n == 0) break;
int cnt = 0;
int s = 0, e = 1, sum = 0;
while(e-1 < now){
if(prime[s] > n) break; //优化
while(sum < n){
sum += prime[e-1];
e++;
}
if(sum == n) cnt++;
if(s < e) sum -= prime[s++]; //s不能超界
}
printf("%d\n", cnt);
}
return 0;
}