[Prime number] How many continuous Prime Number Addition schemes can a number in poj 2739?

Source: Internet
Author: User

A simple question prime number is used to create a table based on the data volume and use the N2 algorithm to traverse a save [k] Prime Number and store the first K prime numbers.

 

#include <iostream>#include <cstdio>#include <memory.h>#include <cmath>using namespace std;const int maxn=10002;int pri[maxn+1];int save[2000+1];int s[2000+1];void make_pri(){    pri[0]=0;    pri[1]=0;    for(int i=2;i<=maxn;i++)    {        pri[i]=1;    }    for(int i=2;i*i<=maxn;i++)    {        if(pri[i])        {            for(int j=i*i;j<=maxn;j+=i)            pri[j]=0;        }    }}void init(){    memset(save,0,sizeof(save));    memset(s,0,sizeof(s));    int i,j=2;    s[1]=2;    for(i=3;i<=maxn;i++)    {        if(pri[i]==1)        {            s[j++]=i;        }    }    for(int i=1;i<=2000;i++)    {        save[i]=save[i-1]+s[i];    } }int main(){    //freopen("in.txt","r",stdin);    memset(save,0,sizeof(save));    make_pri();    init();    int n;    while(cin >> n && n!=0)    {        int cnt=0;        int last;        if(pri[n]==1)        {            for(int i=2000;i>=1;i--)            {                if(s[i]==n)                {                    last=i;                }            }        }        else        {            for(int i=2000;i>=1;i--)            {                if(s[i]<n && s[i+1]>n)                {                    last=i;                }            }        }        for(int i=last;i>=1;i--)        {            for(int j=i-1;j>=0;j--)            {                if(save[i]-save[j]==n)                {                    cnt++;                }            }        }        cout << cnt << endl;    }    return 0;}

 

[Prime number] How many continuous Prime Number Addition schemes can a number in poj 2739?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.