Sum of Consecutive Prime Numbers POJ - 2739
Posted 啦啦啦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sum of Consecutive Prime Numbers POJ - 2739相关的知识,希望对你有一定的参考价值。
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.Input
Output
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<string> 5 #include<cstring> 6 #define MAX 10005 7 using namespace std; 8 9 int prime[MAX],b[MAX]; 10 int k; 11 12 void get_p() 13 { memset(prime,true,sizeof(prime)); 14 prime[1]=false,prime[2]=true; 15 k=1; 16 for(int i=2;i<=MAX;i++){ 17 if(prime[i]){ 18 b[k++]=i; 19 int j=i+i; 20 while(j<=MAX) 21 { prime[j]=false; 22 j=j+i; 23 } 24 } 25 } 26 } 27 int main() 28 { int n; 29 while(~scanf("%d",&n)&&n){ 30 get_p(); 31 // for(int i=1;i<k;i++) printf("%d ",b[i]); 32 int l=1,r=1; 33 int ans=0,sum=0,t=0; 34 while(r<=k){ 35 if(b[r]>n) break; 36 sum+=b[r]; 37 r++; 38 while(sum>n){ 39 sum-=b[l]; 40 l++; 41 } 42 if(sum==n) t++; 43 } 44 printf("%d\n",t); 45 } 46 }
以上是关于Sum of Consecutive Prime Numbers POJ - 2739的主要内容,如果未能解决你的问题,请参考以下文章
[POJ] 2739 Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers POJ - 2739
Sum of Consecutive Prime Numbers(素数打表+尺取)
Greedy:Sum of Consecutive Prime Numbers(POJ 2739)