Sum of Consecutive Prime Numbers

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20050   Accepted: 10989

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

简单打表水题,往下写之前先看看 10000 内的素数有多少个,然后就可以开数组给OJ生成了,或者自己复制粘贴好数组交上去。
得到数组就可以对数组进行游标法遍历了,不难。甚至试除法都可以过= =

 #include <stdio.h>
#include <math.h>
int pr[];
int ispr(int n)
{
int p=sqrt(n);
for(int i=;i<=p;i++)
if(n%i==) return ;
return ;
}
int main()
{
int n,cnt=;
int i,j,c;
long sum; for(i=;i<=;i++)
if(ispr(i))
pr[cnt++]=i; while(~scanf("%d",&n)&&n)
{
c=;
for(i=;i<cnt&&pr[i]<=n;i++)
{
sum=;
for(j=i;j<cnt;j++)
{
sum+=pr[j];
if(sum>=n) break;
}
if(sum==n) c++;
}
printf("%d\n",c);
}
return ;
}

POJ 2739. Sum of Consecutive Prime Numbers的更多相关文章

  1. POJ.2739 Sum of Consecutive Prime Numbers(水)

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

  2. POJ 2739 Sum of Consecutive Prime Numbers(素数)

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

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

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

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

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

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

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

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

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  7. poj 2739 Sum of Consecutive Prime Numbers 小结

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

  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 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

随机推荐

  1. C# windows服务制作(包括安装及卸载)

    开篇语 因工作内容需要做一个windows服务,此前并没有相关经验,所以做了一个demo来跑跑这个梗(高手跳过,需要的来踩)- 效果如下:打开服务,可以找到我们新增的一个windows服务,这个dem ...

  2. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  3. C++服务器开发之笔记三

    为什么需要原子性操作? 我们考虑一个例子:(1)x++这个常见的运算符在内存中是怎样操作的?从内存中读x的值到寄存器中,对寄存器加1,再把新值写回x所处的内存地址 若是有两个线程同时对同一个变量++, ...

  4. PHP资源列表

    一个PHP资源列表,内容包括:库.框架.模板.安全.代码分析.日志.第三方库.配置工具.Web 工具.书籍.电子书.经典博文等等. 初始翻译信息来自:<推荐!国外程序员整理的 PHP 资源大全& ...

  5. 使用ETag进行session的降级

    回顾 在web后台开发中我们经常需要存储一些变量到session中进行暂存,最为特殊的就是"购物车",由于http的无状态特性,因此我们需要在客户端打上一个标记,唯一的标示客户端并 ...

  6. html5 canvas常用api总结(一)

    1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...

  7. 在thinkphp中,写的博文标签多对多关系的标签频率统计算法

    常常看到别人的博客里面,或者网站里面有这样随机颜色,但字体大小与标签出现频率有关的标签云,于是自己就想写一个.至于颜色的随机显示,那就很简单了,这里就不列代码. 因为正在学thinkphp,所以数据查 ...

  8. iOS: 在iPhone和Apple Watch之间共享数据: App Groups

    我们可以在iPhone和Apple Watch间通过app groups来共享数据.方法如下: 首先要在dev center添加一个新的 app group: 接下来创建一个新的single view ...

  9. Entity Framework 教程——安装Entity Framework环境

    安装Entity Framework环境 Entity Framework 5.0 API分布在两个地方,一个可在NuGet包管理器中找到,一个存在于.NET framework中..NET fram ...

  10. const 与 readonly知多少

    原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...