# 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和（滑动窗口）

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 100000 + 10;
int vis[maxn];
int prime[maxn],n,cnt;
void init(){
int m = sqrt(maxn + 0.5);
for (int i = 2; i <= m; ++i) if (!vis[i])
for (int j = i * i; j <= maxn; j += i)vis[j] = 1;
for (int i = 2; i < maxn; ++i)if (!vis[i])prime[cnt++] = i;
//    for (int i = 0; i < 100; ++i)printf("%d\n",prime[i]);
}
int solve(){
int l =0,r = 0;
int sum = 0;
int ans = 0;
while(1){
while(sum < n && prime[r] <=n){
sum += prime[r++];
}
if (l > r)return ans;
if (sum < n)return ans;
if (sum == n){
++ans;
//            printf("%d %d\n",l,r);
sum -= prime[l++];
}
while(sum > n){
sum -= prime[l++];
}
if (sum > n)return ans;
}
}
int main(){
init();
while(scanf("%d",&n) == 1 && n)
printf("%d\n",solve());
return 0;
}


#### UVA 1210 Sum of Consecutive Prime Numbers（数论）

2015-05-07 01:26:05

#### Sum of Consecutive Prime Numbers----暴力水题

2017-04-10 15:12:26

#### 解题报告_sum of consecutive prime numbers

2018-01-14 22:10:51

#### 北大ACM2739——Sum of Consecutive Prime Numbers

2015-07-08 13:31:23

#### Sum of Consecutive Prime Numbers UVA - 1210 连续素数之和

2017-07-20 18:04:49

#### UVa 1210 - Sum of Consecutive Prime Numbers（滑动窗口）

2014-08-15 09:27:29

#### 1210 - Sum of Consecutive Prime Numbers（数学水题）

2015-09-03 09:41:32

#### LeetCode 829. Consecutive Numbers Sum

2018-05-11 21:06:45

#### UVa 1210 - Sum of Consecutive Prime Numbers（素数＋连续和）

2015-02-15 17:03:17

#### Prime Gap（素数表+二分）

2014-09-15 15:34:38