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 = ;
int main()
{
bool vis[MAXN];
int isprime[MAXN],k=;
memset(vis,,sizeof(vis));
for(int i=;i<(int)sqrt((double)MAXN);i++) //筛选法挑选素数
{
if(!vis[i])
{
for(int j=i*i;j<MAXN;j+=i)
vis[j]=;
}
}
for(int i=;i<MAXN;i++)
{
if(!vis[i])
isprime[k++]=i;
}
int n;
while(cin>>n)
{
if(n==)
break;
//因为不一定是从头开始,所以需要两层循环,
//每一个i都需要从i开始往后累加
int num=;
for(int i=;isprime[i]<=n;i++) //isprime循环
{
int ans = ; //累加器
for(int j=i ; j<k&& ans<n ; j++) //从每一个i开始往后循环
{
ans += isprime[j];
}
if(ans==n)
num++;
}
cout<<num<<endl;
}
return ;
}

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

//memory :736K time :32MS
#include<iostream>
using namespace std;
const int MAXN = ;
int prime[MAXN],prime_num = ; bool isprime(int n)
{
for(int i=;i<prime_num;i++)
{
if(n%prime[i]==)
return false;
}
return true;
} int main()
{
int n; for(int i=;i<MAXN;i++)
{
if(isprime(i))
{
prime[prime_num++]=i;
}
}
while(cin>>n)
{
if(n==)
break;
int num=;
for(int i=;prime[i]<=n;i++)
{
int ans = ;
for(int j=i;j<prime_num&&ans<n;j++)
{
ans += prime[j];
}
if(ans == n)
num++;
}
cout<<num<<endl;
}
return ;
}

poj2739的更多相关文章

  1. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  2. poj2739 poj2100 尺取法基础(二)

    都是很简单的题目 poj2739素数打表+单点推移 #include<iostream> #include<cstring> #include<cstdio> us ...

  3. POJ2739解题报告

    2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer: ...

  4. POJ2739 - Sum of Consecutive Prime Numbers(素数问题)

    题目大意 给定N,要求你计算用连续的素数的和能够组成N的种数 题解 先筛选出素数,然后暴力判断即可... 代码: #include<iostream> #include<cstrin ...

  5. 【POJ2739】Sum of Consecutive Prime Numbers

    简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...

  6. poj2739尺取法+素数筛

    Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...

  7. poj2739(尺取法+质数筛)

    题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...

  8. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25225 ...

  9. POJ2739(尺取法)

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23931 ...

随机推荐

  1. Window的匿名Closing 事件

    group.Closing += (sender, e) => { try {   Code here } } catch (Exception ex) { Exception here } } ...

  2. Struts2中将.action改为.do

    struts2中action的默认拓展名是".action",而之前的拓展名一直为".do",工作中需要要把struts2的action拓展名改为". ...

  3. 一个js编写全选、弹出对话框、ajax-json的案例

    js功能有:全选.弹出对话框.使用json传输ajax数据:不想在写多余的文字了,直接上代码: <%@ page language="java" contentType=&q ...

  4. JMeter 参数化、检查点、集合点

      参数化:简单的来理解一下,我们录制了一个脚本,这个脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密 ...

  5. JavaScript学习笔记之 数组方法一 堆栈 和队列

    数组的方法 以及 堆栈的操作的方法 JavaScript是一种弱类型语言,不像其它程序语言需要严格定义数据类型.在JavaScript中数组可以任意修改变动,这样也就出现了一个问题,如果边遍历数组边操 ...

  6. CentOS 6.5 升级内核 kernel

    本文适用于CentOS 6.5, CentOS 6.6,亲测可行,估计也适用于其他Linux发行版. 1. 准备工作 1.1 下载源码包 Linux内核版本有两种:稳定版和开发版 ,Linux内核版本 ...

  7. php不会的点

    1.DIRECTORY_SEPARATOR:DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量,不需要任何定义与包含即可直接使用 ...

  8. WebApi2官网学习记录---Attribute Routing

    从WebApi 1迁移到WebAPI 2要改变配置代码如下: WebApi 1: protected void Application_Start() { // WARNING - Not compa ...

  9. 【3】创建一个简单的Laravel例子

    现在我们来创建一个Laravel的例子来帮助理解 1.首先打开app/Http/routes.php文件,在里边写上一条路由: 2.创建一个控制器,有两种方法 ①在app/Http/Controlle ...

  10. 关于jquery-easyUI中主键属性data-options的了解

    data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中,例如: <div class=& ...