Sum of Consecutive Prime Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20560 | Accepted: 11243 |
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
题目大意:
这道题是说,我给你一个数字n,然后你求出n==prime[i]+prime[i+1]+prime[i+2]+...+prime[i+tt]的方案数。
解题思路:
很水的一道题,上来后先打一个10000以内的素数表,然后你只需要处理好素数串的头和尾就行了,什么是头和尾呢?
就是for( int i = 1;prime[i] <= n;i++) ,因为我们这样想啊,如果prime[i] > n 的话,肯定是直接退出的,不用在判断了
然后内部的判断用一个tt变量边走边标记就好
代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<fstream> 4 # include<algorithm> 5 # include<functional> 6 # include<cstring> 7 # include<string> 8 # include<cstdlib> 9 # include<iomanip> 10 # include<numeric> 11 # include<cctype> 12 # include<cmath> 13 # include<ctime> 14 # include<queue> 15 # include<stack> 16 # include<list> 17 # include<set> 18 # include<map> 19 20 using namespace std; 21 22 const double PI=4.0*atan(1.0); 23 24 typedef long long LL; 25 typedef unsigned long long ULL; 26 27 # define inf 999999999 28 # define MAX 10000+4 29 30 int prime[MAX]; 31 int book[MAX]; 32 int len = 0; 33 34 void init() 35 { 36 for ( int i = 2;i <= MAX;i++ ) 37 { 38 book[i] = 1; 39 } 40 for ( int i = 2;i <= MAX;i++ ) 41 { 42 if ( book[i]==1 ) 43 { 44 for ( int j = 2*i;j <= MAX;j+=i ) 45 { 46 book[j] = 0; 47 } 48 } 49 } 50 len = 0; 51 for ( int i = 2;i <= MAX;i++ ) 52 { 53 if ( book[i]==1 ) 54 { 55 prime[++len] = i; 56 } 57 } 58 // for ( int i = 1;i <= len;i++ ) 59 // { 60 //// cout<<prime[i]<<" "; 61 // } 62 // cout<<endl; 63 64 } 65 66 67 int main(void) 68 { 69 init(); 70 // cout<<prime[1]<<endl; 71 int n; 72 while ( cin>>n ) 73 { 74 if ( n==0 ) 75 break; 76 77 int tot = 0; 78 int temp = 0; 79 int j = 1; 80 81 for ( int i = 1;prime[i] <= n;i++ ) 82 { 83 temp = 0; 84 for ( int tt = i;tt <= len;tt++ ) 85 { 86 temp+=prime[tt]; 87 if ( temp==n ) 88 { 89 tot++; 90 break; 91 } 92 if ( temp > n ) 93 { 94 break; 95 } 96 } 97 } 98 99 cout<<tot<<endl; 100 101 } 102 103 104 105 return 0; 106 }