poj2739

Sum of Consecutive Prime Numbers










Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18427   Accepted: 10122

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

过的第一段代码,可是后来发现自己事实上是蠢了,因为根本没必要用筛选法挑选素数,以至于我wrong了几次,因为筛选法挑出来的素数在数组中不是连续存在的,如果不再把素数统一赋值到一个连续的数组中的话,我们在后面进行累加的时候就会出现一些困难,因为我们还需要给vis一个循环,让其过滤掉非素数。我wrong了一次后发现了这个问题,于是赋值到一个连续的数组中,当时我选择的是在筛选素数的同时进行赋值,可是wrong了几次,后来发现,在筛选的过程中赋值,isprimer数组中的素数是不完整的,因为筛选法本身就不是全部遍历筛选的!


//memory:744K time:0MS
#include <iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int MAXN = 10001;
int main()
{
bool vis[MAXN];
int isprime[MAXN],k=0;
memset(vis,0,sizeof(vis));
for(int i=2;i<(int)sqrt((double)MAXN);i++) //筛选法挑选素数
{
if(!vis[i])
{
for(int j=i*i;j<MAXN;j+=i)
vis[j]=1;
}
}
for(int i=2;i<MAXN;i++)
{
if(!vis[i])
isprime[k++]=i;
}
int n;
while(cin>>n)
{
if(n==0)
break;
//因为不一定是从头开始,所以需要两层循环,
//每一个i都需要从i开始往后累加
int num=0;
for(int i=0;isprime[i]<=n;i++) //isprime循环
{
int ans = 0; //累加器
for(int j=i ; j<k&& ans<n ; j++) //从每一个i开始往后循环
{
ans += isprime[j];
}
if(ans==n)
num++;
}
cout<<num<<endl;
}
return 0;
}

以下给出一个较好的代码,挑选素数的想法很好,素数只能被素数整除,不会被偶数整除。但是这种方法很明显慢一些


//memory :736K time :32MS
#include<iostream>
using namespace std;
const int MAXN = 10001;
int prime[MAXN],prime_num = 0;

bool isprime(int n)
{
for(int i=0;i<prime_num;i++)
{
if(n%prime[i]==0)
return false;
}
return true;
}

int main()
{
int n;

for(int i=2;i<MAXN;i++)
{
if(isprime(i))
{
prime[prime_num++]=i;
}
}
while(cin>>n)
{
if(n==0)
break;
int num=0;
for(int i=0;prime[i]<=n;i++)
{
int ans = 0;
for(int j=i;j<prime_num&&ans<n;j++)
{
ans += prime[j];
}
if(ans == n)
num++;
}
cout<<num<<endl;
}
return 0;
}

poj2739

时间: 2024-11-20 02:08:21

poj2739的相关文章

POJ2739解题报告

2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer:pprp @declare: @begin:11:50 @end:16:59 @error:各种取等条件,包括审题上也出现了重大失误 @date:2017/9/1 */ #include <iostream> #include <cstdio> #include <cmath&g

【POJ2739】Sum of Consecutive Prime Numbers

简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cctype> 7 #include <complex.h> 8 #

POJ2739(尺取法)

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

Sum of Consecutive Prime Numbers(poj2739)

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

poj2739尺取法+素数筛

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 intege

poj2739 Sum of Consecutive Prime Numbers (素数打表)

D - Sum of Consecutive Prime Numbers Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2739 Appoint description: System Crawler (2016-05-05) Description Some

poj2739(尺取法+质数筛)

题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algorithm> #include<cstring> #define maxn 1000005 using namespace std; int visit[maxn];int prime[maxn]; void qprime() { memset(visit,0,sizeof(visit));

poj2739-Sum of Consecutive Prime Numbers

题目链接 http://vjudge.net/problem/POJ-2739 解题思路 先用筛法筛出素数,然后枚举就行了. 代码 #include<stdio.h> #include<string.h> #include<math.h> #define MAX_SIZE 10005 int primeNumber[MAX_SIZE]; bool judgePrime[MAX_SIZE]; int tot; int search(int number) { int co

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po