Sum of consecutive Prime Numbers
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 22019 |
|
Accepted: 12051 |
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 Idea: First of all to filter out 0 to 10000 of the prime, and then the way to run it again.
1 ImportJava.util.*;2 Importjava.lang.*;3 ImportJava.io.*;4 5 Public classMain {6 Public Static voidMainFinalstring[] args) {7Scanner in =NewScanner (system.in);8 intN, I, J, K, p, Q;9 intAa[] =New int[20000];Ten intBb[] =New int[20000]; OneArrays.fill (AA, 0); AAA[1] = 1; -Aa[0] = 1; - intCNT = 0; the for(i = 2; I <=; i++) { - if(Aa[i] = = 0) { - for(j = i; I * J <= 10000; J + +) { -Aa[i * j] = 1; + } - } + A } at for(i = 0; I <= 10000; i++) { - if(Aa[i] = = 0) { -bb[cnt++] =i; - } - } - intSS = 1; in while(ss = = 1) { -K =in.nextint (); to intAns = 0; + if(k = = 0) { - Break; the}Else { * intsum = 0; $ intLL = 0;Panax Notoginseng intRR = 0; - while(ss = = 1) { the if(RR >CNT) { + Break; A } the if(BB[RR] >k) { + Break; - } $ if(LL >RR) { $ Break; - } - while(ss = = 1) { theSum + =BB[RR]; - if(Sum >=k) {Wuyi Break; the}Else { -rr++; Wu } - if(RR >CNT) { About Break; $ } - } - if(Sum = =k) { -ans++; A } +Sum-=Bb[ll]; theSum-=BB[RR]; -ll++; $ } the } the System.out.println (ans); the } the return; - } in}
View Code
Sum of consecutive Prime Numbers (poj2739)