POJ 2739 Sum of consecutive Prime Numbers ruler

Source: Internet
Author: User
Tags cmath

Sum of consecutive Prime Numbers

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21924 Accepted: 11996

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.
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 2005test instructions; Enter a number (<=1E5) to find that the number can be composed of several successive primes in the prime list;Error Analysis: Know that you can use the ruler method, the movement of the left and right ends of the ruler method is also mastered better, that is, when the problem of exiting the circulation is relatively bad, so re once;The first is the AC code, written by yourself:
#include <cstdio> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib > #include <cmath> #include <vector> #include <queue> #include <map> #include <algorithm > #include <set>using namespace std; #define MM (a) memset (A,0,sizeof (a)) typedef long Long Ll;typedef unsigned  Long long ull;const int mod = 1000000007;const double EPS = 1e-10;const int inf = 0x3f3f3f3f;bool prime (int n) {for (int    i=2;i*i<=n;i++) if (n%i==0) return 0; return 1;}    int a[2000];void init () {a[0]=0; for (int i=2,cnt=0;i<=10000;i++) if (prime (i)) a[++cnt]=i;}    int main () {init ();    int n;        while (~SCANF ("%d", &n) &&n) {int l=1,r=0,sum=0,ans=0;        for (;;) {while (sum<n&&a[r+1]<=n)/*a[r+1]<=n represents
The number is additive, meaning that the right end point can also continue to move right/sum+=a[++r]; The IF (sum<n)//Right endpoint cannot continue to the right, and the right side of the left endpoint only causes the sum
Decrease, meaning that the sum array can no longer be greater than or equal to N, it exits the loop */break; else if (sum>n) {sum-=a[l]; l++; } else if (sum==n) {ans++; SUM-=A[L]; l++; }} printf ("%d\n", ans); } return 0;}

Here is the re code, to understand why the RE:

#include <cstdio> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib > #include <cmath> #include <vector> #include <queue> #include <map> #include <algorithm > #include <set>using namespace std; #define MM (a) memset (A,0,sizeof (a)) typedef long Long Ll;typedef unsigned  Long long ull;const int mod = 1000000007;const double EPS = 1e-10;const int inf = 0x3f3f3f3f;bool prime (int n) {for (int    i=2;i*i<=n;i++) if (n%i==0) return 0; return 1;}    int a[2000];void init () {a[0]=0; for (int i=2,cnt=0;i<=10000;i++) if (prime (i)) a[++cnt]=i;}    int main () {init ();    int n;        while (~SCANF ("%d", &n) &&n) {int l=1,r=0,sum=0,ans=0;        for (;;) {While (sum<n) 
/* If you do not add a[r]<=n, it will continue to be added,
That is, the right endpoint has moved to the right, thus exploding the array */SUM+=A[++R]; if (sum<n) break; else if (sum>n) {sum-=a[l]; l++; } else if (sum==n) {ans++; SUM-=A[L]; l++; }} printf ("%d\n", ans); } return 0;}

  

POJ 2739 Sum of consecutive Prime Numbers ruler

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.