首页 > 学院 > 开发设计 > 正文

Poj 2739

2019-11-14 09:02:30
字体:
来源:转载
供稿:网友

http://poj.org/PRoblem?id=2739

Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24310 Accepted: 13228

Description

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

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2317412066612530

Sample Output

11230012

Source

Japan 2005筛法求素数 求连续素数的和=?n
#include<iostream>using namespace std;int a[10001]={0},i,j;void primer(int a[]){	for(i=2;i<10001;i++)	   for(j=i+i;j<10001;j+=i)	   {	   	  if(j%i==0) a[j]=1;	   }	}int main(){	int p[10001],t=0;	primer(a);	for(i=2;i<10001;i++)	{		if(a[i]==0) p[t++]=i;	}	/*cout<<t<<endl;	for(i=0;i<t;i++)	  cout<<p[i]<<" ";*/	int n;	while(cin>>n,n!=0)	{		int sum=0,temp=0,m=0;		for(i=0;i<10001;i++)		{   if(p[i]>n) break;		    sum=0;		   for(j=i;j<10001;j++)		   {		   	 sum+=p[j];		   	 if(sum==n) m++;		   	 if(sum>n) break;		  }  			}		cout<<m<<endl;	}	}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表