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 long i,j;  // !!注意
    nprime = 1;
    memset(isprime,true,sizeof(isprime));
    isprime[1] = 0;
    for(i = 2;i<=N;i++)
    {
        if(isprime[i])
        {
            prime[nprime++] = i;
            for(j = i*i;j<=N;j+=i)
            {
                isprime[j] = false;
            }
        }
    }
}

int main()
{
	int a,i,j;
	int sum,count;
	doprime();
	while(cin>>a)
	{

		count = 0;
		if(a == 0) break;
		for(i = 1;i<=1300;i++)
		{
			sum = 0;
			for(j = i;j<=1300;j++)
			{
				sum+=prime[j];
				if(sum == a)
					count++;
				if(sum > a)
					break;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}
时间: 2024-10-08 01:59:50

POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)的相关文章

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(素数)

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

ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representation

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(水题)

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 尺取法

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 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 难度: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