Sum of consecutive Prime Numbers
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 21613 |
|
Accepted: 11837 |
Description
Some positive integers can be is represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer has? For example, the integer is has a representations 5 + 7 + one + + + 53. The integer 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 has no such representations. Note that Summands must is consecutive prime
Numbers, so neither 7 + or 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program this reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers, each with a separate line. The integers is between 2 and ten, inclusive. The end of the input is indicated by a zero.
Output
The output should is composed of lines each corresponding to a 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 Nu Mbers. No other characters should is inserted in the output.
Sample Input
2317412066612530
Sample Output
11230012
Source
Japan 2005 Experience: The first loop A is written in n;t half-day; Choose the shortest one when choosing a loop;
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <stack>Ten using namespacestd; One #defineN 10000 A intN,m,pr[n],a=0; - BOOLP[n]; - voidgcd () { the for(intI=2; i<n;i++){ - if(!p[i]) {pr[a++]=i;} - for(intj=i*i;j<n;j+=i) -p[j]=true; + } - } + A intMain () { at //freopen ("In.txt", "R", stdin); -Memset (P,false,sizeof(P)); - gcd (); - while(cin>>n,n) { - intj=0, sum;m=0; - for(intI=0; i<a;i++){ insum=0; j=i; - while(sum<N) { tosum+=pr[j++]; + } - if(sum==N) them++; * } $printf"%d\n", m);Panax Notoginseng } - return 0; the}
POJ 2739 (Sieve method to calculate prime numbers)