poj 2739 Sum of Consecutive Prime Numbers 尺取法

Sum of Consecutive Prime Numbers

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

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

Source

Japan 2005

题意;输入一个数字(<=1e5)求该数可由几种在素数表中连续的素数之和组成;

错因分析:知道可以使用尺取法,对尺取法左右两端点的移动也掌握的比较好,就是在什么时候退出循环的问题上处理的比较糟糕,所以re了一次;

第一份是AC代码,自己写的:

#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表示该数是可加的,意即右端点还可以继续右移*/
                sum+=a[++r];
            if(sum<n)/*右端点无法继续右移,而左端点的右移只能使sum减小,意即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;
}

  下面是re代码,好好体会下为什么会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) /*如果不添加a[r]<=n的话,会一直加下去,即右端点一直往右移动,从而爆数组*/
                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;
}

  

时间: 2024-10-24 09:58:49

poj 2739 Sum of Consecutive Prime Numbers 尺取法的相关文章

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20020   Accepted: 10966 Description Some positive integers can be represented by a sum of one or more consecutive

POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2739 Sum of Consecutive Prime Numbers(尺取法)

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

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p

POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)

[题意简述]:题意很简单,就是用连续的素数加和,计算有多少个这样的连续的素数数列可以使这个序列的和等于输入的数. [分析]:很经典的素数模板,基本所有有关素数的题,我都会使用这个模板. // 268K 16Ms #include<iostream> using namespace std; #define N 10000 bool isprime[N]; long long prime[1300],nprime; // 注意long long void doprime() { long lon

POJ 2739 Sum of Consecutive Prime Numbers 难度:0

题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace std; int method[10001][1300]; int dp[10001]; bool isntprime[10001]; int heap[1300],cnt; void calprime(){ method[2][0]=1; dp[2]++; heap[cnt++]=2; for(int i

POJ 2739 Sum of Consecutive Prime Numbers

题意:问一个数字能被多少种连续的质数相加得到. 解法:一开始没看见是连续的……SB了半天……后来打了个表……就这样吧 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h>