POJ 2739Sum of Consecutive Prime Numbers(尺取)

来源:互联网 发布:庞氏骗局知乎 编辑:程序博客网 时间:2024/06/06 19:53
Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26281 Accepted: 14252

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
题意: 一个数可以用素数表示的话  ,可以有不同的素数表示形式,给你一个数x  你的任务是找出来  有多少种 素数的表示
形式。
思路: 打表将可能的素数打出来,然后就是一个尺取的模板题。
代码: 
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define N 10005///////    2739using namespace std;int prime[N];int a[N],sum[N];int n;void init(){int i,j;memset(prime,1,sizeof(prime));prime[0]=prime[1]=0;for(i=2;i<=10002;i++){if(!prime[i]) continue;for(j=2*i;j<=10002;j+=i){prime[j]=0;}}n=0;for(i=1;i<=10002;i++) if(prime[i]) a[++n]=i;for(i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];//for(i=1;i<=n;i++) printf("%d ",sum[i]);return ;}void solve(int x){int left,right;int sum=0;int cnt=0;left=right=1;while(left<=n){while(right<=n&&sum<x){sum+=a[right++];if(sum==x) cnt++;}sum-=a[left++];if(sum==x) cnt++;}printf("%d\n",cnt);return ;}int main(){init();int x;while(~scanf("%d",&x)){if(x==0) break;solve(x);}return 0;} 


原创粉丝点击