码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2739 Sum of Consecutive Prime Numbers

时间:2015-11-23 09:56:20      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

素数非负使得前缀和单调,于是对于固定的区间尾部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;
}

 

POJ 2739 Sum of Consecutive Prime Numbers

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4975641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!