将一个数分解为连续素数的和。
/*Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 5777 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 */ #include <stdio.h> #include <string.h> #include "math.h" #define MAX_INPUT_NUMBER 10001 #define MAX_CONSECUTIVE_PRIME_NUMBER 1230 #define PRIME_TRUE 1 #define PRIME_FALSE 0 typedef unsigned char PRIME_BOOL; PRIME_BOOL gabPrimeList[MAX_INPUT_NUMBER]; int gaiConsecutivePrime[MAX_CONSECUTIVE_PRIME_NUMBER]; int SumofConsecutivePrimeNumbersmain(void) { int iPrimeNum = 0; int iInteger; int iMaxFactor; int iLoop; int iLoopHead; int iLoopTail; int iSum; int iTimeNum; memset(gabPrimeList,PRIME_FALSE,MAX_INPUT_NUMBER*sizeof(PRIME_BOOL)); gabPrimeList[2] = PRIME_TRUE; gaiConsecutivePrime[iPrimeNum] = 2; iPrimeNum++; for (iInteger = 3; iInteger < MAX_INPUT_NUMBER; iInteger+=2 ) { iMaxFactor = (int)sqrt(iInteger); for (iLoop = 3; iLoop <= iMaxFactor; iLoop+=2) { if (0 == iInteger%iLoop) { break; } } if (iLoop > iMaxFactor) { gaiConsecutivePrime[iPrimeNum] = iInteger; iPrimeNum++; gabPrimeList[iInteger] = PRIME_TRUE; } } while(1) { scanf("%d",&iInteger); if (0 == iInteger) { break; } iSum = 0; iTimeNum = 0; for (iLoopHead = 0, iLoopTail = 0; iLoopHead <= iPrimeNum,gaiConsecutivePrime[iLoopTail] <= iInteger/2;) { iSum += gaiConsecutivePrime[iLoopHead]; if (iSum == iInteger) { iTimeNum++; iSum -= gaiConsecutivePrime[iLoopTail]; iLoopTail++; iLoopHead++; } else if (iSum > iInteger) { while (iSum > iInteger) { iSum -= gaiConsecutivePrime[iLoopTail]; iLoopTail++; } if (iSum == iInteger) { iTimeNum++; iSum -= gaiConsecutivePrime[iLoopTail]; iLoopTail++; iLoopHead++; } else { iLoopHead++; } } else// (iSum < iInteger) { iLoopHead++; } } if (gabPrimeList[iInteger] == PRIME_TRUE) { iTimeNum++; } printf("%d/n",iTimeNum); } return 0; }