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

时间:2021-11-25 22:42:39

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数

用的筛法素数打表

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 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int prime[10000],num[10005];
int main()
{
int i,j,k=0,tmp,n;
for(i=0;i<=10000;i++) num[i]=1;
num[0]=0;
num[1]=0;
for(i=2;i<=10000;i++)
{
if(num[i])
{
prime[k++]=i;
for(tmp=i*2;tmp<=10000;tmp+=i)
num[tmp]=0;
}
}
while(scanf("%d",&n)!=EOF&&n)
{
int ans=0,sum;
for(i=0;i<k;i++)
{
sum=0;
for(j=i;j<k&&sum<n;j++)
sum+=prime[j];
if(sum==n)
ans++;
}
printf("%d\n",ans);
}
}

  

POJ 2739 Sum of Consecutive Prime Numbers【素数打表】的更多相关文章

  1. POJ 2739 Sum of Consecutive Prime Numbers&lpar;素数&rpar;

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  2. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度&colon;0

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

  3. POJ&period;2739 Sum of Consecutive Prime Numbers&lpar;水&rpar;

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

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

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  5. POJ 2739 Sum of Consecutive Prime Numbers&lpar; &ast;【素数存表】&plus;暴力枚举 &rpar;

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

  6. POJ 2739&period; Sum of Consecutive Prime Numbers

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

  7. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  8. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. h5曲线滑动确认

    h5项目需根据几条弯曲的线条让用户进行曲线式滑动,滑动时需实时响应某些样式,于是就有了下面这个实例.(可自定义多个子对象大小分别放在线条各处,以增加曲线滑动确认精度.) <!doctype ht ...

  2. Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片

    设置 背景颜色和背景图片 首先设置autoFillBackground属性为真然后定义一个QPalette对象设置QPalette对象的背景属性(颜色或图片)最后设置QWidget对象的Palette ...

  3. Spring的定时任务配置2(转)

    spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTas ...

  4. android - startActivity浅谈

    当执行startActivity(Intent intent, Bundle options)函数的时候,应用程序不是直接呼叫另外一个Activity,而是将intent传进Android框架中.An ...

  5. javascript从定义到执行 js引擎 闭包

    javascript从定义到执行,JS引擎在实现层做了很多初始化工作,因此在学习JS引擎工作机制之前,我们需要引入几个相关的概念:执行环境 栈.全局对象.执行环境.变量对象.活动对象.作用域和作用域链 ...

  6. java集合(list&comma;set&comma;map&rpar;

    集合 集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使用. ...

  7. 记录一次mongodb因网络问题导致shard节点异常

    现象: 机房反馈9点左右,机房交换机故障,导致网络出现问题 业务人员反馈某个接口超时 初查:通过业务日志查看分析发现,在连接mongo的某个collections时候,报错错误如下: 在写入数据的时候 ...

  8. Could not initialize class utils&period;JdbcUtils

    今天用JdbcUtils时出现了一个问题,被困扰了一晚上.从网上找的原因,说什么url错了,版本不一致等等都不能解决我的问题, 我写好了一个JdbcUtils准备测试,发现从后台用Dao测试完全没问题 ...

  9. Python 基础知识(一)

    1.Python简介 1.1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时 ...

  10. poj2752seek the name&comma; seek the fame【kmp】

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...