Sum of prime numbers
Some integers can be expressed as a sum of successive primes, given an integer to find out the number of consecutive integer sequences that can represent this integer
Methods: The table is played and then the Vernier caliper method can be used
#include <iostream>#include<functional>#include<algorithm>#defineMax_n 10010using namespacestd;Static intPrimes_set[max_n], flag[max_n], p_sum;voidInivilize (void);voidSolveConst int);intMainvoid) {inivilize (); intN; while(~SCANF ("%d", &N)) {if(n = =0) Break; Solve (n); } returnexit_success;}voidSolveConst intN) { intS, T, sum, ans =0; S= t = SUM =0; while(1) { while(Primes_set[t] <= n && Sum <N) Sum+ = primes_set[t++]; if(Sum = =N) Ans++; if(Sum <N) Break; Sum-= primes_set[s++]; } printf ("%d\n", ans);}voidInivilize (void){ inti =2, J; memset (Flag,0,sizeof(flag)); for(; i < max_n; i++) { if(!Flag[i]) primes_set[p_sum++] =i; for(j =0; J < p_sum && Primes_set[j] * I < max_n; J + +) Flag[primes_set[j]* I] =1; }}
Greedy:sum of consecutive Prime Numbers (POJ 2739)