Saturday, 10 October 2015

UVA 1210 Sum of Consecutive Prime Numbers (UVALive 3399, POJ 2739, Regionals 2005 >> Asia - Tokyo)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 10000

vector<int>prime;
bool status[MX+2];

void PrimeGenerate(int n)
{
    int sq=sqrt(n);
    for(int i=3; i<=sq; i+=2)
    {
        if(!status[i])
            for(int j=i*i; j<=n; j+=(i<<1))
                status[j]=true;
    }

    status[0]=status[1]=true;
    for(int i=4;i<=n;i+=2) status[i]=true;

    prime.push_back(2);
    for(int i=3; i<=n; i+=2)
        if(!status[i])
            prime.push_back(i);
return;
}


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    char ch;
    PrimeGenerate(10000);
    int pl=prime.size();
    while(cin>>n && n)
    {
        int cnt=0;
        for(int i=0;i<pl;i++)
        {
            int tot=0;
            for(int j=i;j<pl;j++)
            {
                tot+=prime[j];
                if(tot==n) cnt++;
                if(tot>=n) break;
            }
        }
        cout<< cnt <<"\n";
    }
return 0;
}

No comments:

Post a Comment