Greedy:sum of consecutive Prime Numbers (POJ 2739)

Source: Internet
Author: User

                

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.