Sum of Consecutive Prime Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21924 | Accepted: 11996 |
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
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
Source
题意;输入一个数字(<=1e5)求该数可由几种在素数表中连续的素数之和组成;
错因分析:知道可以使用尺取法,对尺取法左右两端点的移动也掌握的比较好,就是在什么时候退出循环的问题上处理的比较糟糕,所以re了一次;
第一份是AC代码,自己写的:
#include<cstdio> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include<map> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; bool prime(int n) { for(int i=2;i*i<=n;i++) if(n%i==0) return 0; return 1; } int a[2000]; void init() { a[0]=0; for(int i=2,cnt=0;i<=10000;i++) if(prime(i)) a[++cnt]=i; } int main() { init(); int n; while(~scanf("%d",&n)&&n) { int l=1,r=0,sum=0,ans=0; for(;;) { while(sum<n&&a[r+1]<=n)/*a[r+1]<=n表示该数是可加的,意即右端点还可以继续右移*/ sum+=a[++r]; if(sum<n)/*右端点无法继续右移,而左端点的右移只能使sum减小,意即sum数组无法再大于等于n,就可以退出循环*/ break; else if(sum>n) { sum-=a[l]; l++; } else if(sum==n) { ans++; sum-=a[l]; l++; } } printf("%d\n",ans); } return 0; }
下面是re代码,好好体会下为什么会re:
#include<cstdio> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include<map> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; bool prime(int n) { for(int i=2;i*i<=n;i++) if(n%i==0) return 0; return 1; } int a[2000]; void init() { a[0]=0; for(int i=2,cnt=0;i<=10000;i++) if(prime(i)) a[++cnt]=i; } int main() { init(); int n; while(~scanf("%d",&n)&&n) { int l=1,r=0,sum=0,ans=0; for(;;) { while(sum<n) /*如果不添加a[r]<=n的话,会一直加下去,即右端点一直往右移动,从而爆数组*/ sum+=a[++r]; if(sum<n) break; else if(sum>n) { sum-=a[l]; l++; } else if(sum==n) { ans++; sum-=a[l]; l++; } } printf("%d\n",ans); } return 0; }