POJ 2739 Sum of Consecutive Prime Numbers
素数非负使得前缀和单调,于是对于固定的区间尾部j,头部i也是单调的,区间和更新的时候维护一下就好。
/********************************************************* * ------------------ * * author AbyssalFish * **********************************************************/ #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<queue> #include<vector> #include<stack> #include<vector> #include<map> #include<set> #include<algorithm> #include<cmath> #include<numeric> using namespace std; const int maxn = 1e4, pi_n = 1229; int pm[pi_n]; bool vis[maxn+1]; void sieve(int n = maxn) { //cout<<n<<endl; int m = 100; for(int i = 2; i <= m; i++) if(!vis[i]){ for(int j = i*i; j <= n; j+=i) vis[j] = true; } int k = 0; for(int i = 2; i <= n; i++)if(!vis[i]){ pm[k] = i; k++; //cout<<i<<endl; } //cout<<k; } //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif sieve(); int n; while(scanf("%d",&n),n){ int ans = 0, sum = 0; for(int i = 0, j = 0; j < pi_n && pm[j] <= n; j++){ sum += pm[j]; while(sum > n) sum -= pm[i++]; if(sum == n) ans++; } printf("%d\n", ans); } return 0; }