Sum of Consecutive Prime Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21247 | Accepted: 11625 |
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.
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
一开始迷之wa...
先找出素数下标的上界就可以A...
然后纠结了20分钟...
然后发现是预处理的素数少了一个素数..
我预处理是处理到<10005的素数...
最大数10000,而超过10000的第一个素数是10007
这样判断终止条件就会死循环...
sad
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /************************************************************************* 2 > File Name: code/poj/2739.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月25日 星期五 01时32分43秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 #define lr dying111qqz 26 using namespace std; 27 #define For(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 int pri[10005]; 32 int n ; 33 int mx; 34 bool prime ( int n) 35 { 36 if (n<=3) return true; 37 for ( int i = 2 ; i*i<= n ; i++) 38 { 39 if (n%i==0) return false; 40 } 41 return true; 42 } 43 44 void solve() 45 { 46 int head = 1; 47 int tail = 1; 48 int sum = 0 ; 49 int ans = 0 ; 50 while (pri[tail]<=n) 51 { 52 // cout<<"asd"<<endl; 53 sum = sum + pri[tail]; 54 if (sum>=n) 55 { 56 while (sum>n) 57 { 58 sum = sum - pri[head]; 59 head++; 60 } 61 62 if (sum==n) 63 { 64 ans++; 65 } 66 } 67 tail++; 68 } 69 printf("%d ",ans); 70 } 71 int main() 72 { 73 #ifndef ONLINE_JUDGE 74 // freopen("in.txt","r",stdin); 75 #endif 76 77 int cnt = 0 ; 78 for ( int i = 2 ; i <= 10011 ; i++) 79 { 80 if (prime(i)) 81 { 82 cnt++; 83 pri[cnt] = i ; 84 // if (i>10000) cout<<i<<endl; 85 } 86 } 87 while (scanf("%d",&n)!=EOF&&n) 88 { 89 solve(); 90 } 91 92 #ifndef ONLINE_JUDGE 93 fclose(stdin); 94 #endif 95 return 0; 96 }