Sum of Consecutive Prime Numbers
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 19350
Accepted: 10619

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

题目大意:

一个数能够由若干种连续的素数序列求和得到,比方说41 = 2+3+5+7+11+13 = 11+13+17 = 41

共同拥有三种不同的素数序列求和得到。给你一个数N,求满足N = 连续的素数序列和的方案数

思路:

非常easy的题目。可是用普通方法推断素数可能会超时,这里用了筛法求素数的方法直接用数组Prime

推断是否为素数,另开一个数组PrimeNum用来存全部的素数。

最后就是枚举,求得满足的方案数

#include<stdio.h>
#include<string.h> int Prime[10010],PrimeNum[10010]; int IsPrime()//筛法求素数
{
Prime[0] = Prime[1] = 0; for(int i = 2; i <= 10000; i++)
Prime[i] = 1;
for(int i = 2; i <= 10000; i++)
{
for(int j = i+i; j <= 10000; j+=i)
Prime[j] = 0;
}
int num = 0;
for(int i = 0; i <= 10000; i++)
if(Prime[i])
PrimeNum[num++] = i; return num;
}
int main()
{
int num = IsPrime();
int N;
while(~scanf("%d",&N) && N!=0)
{
int count = 0;
for(int i = 0; PrimeNum[i]<=N && i < num; i++)//枚举
{
int sum = 0;
for(int j = i; PrimeNum[j]<=N && j < num; j++)
{
sum += PrimeNum[j];
if(sum == N)
{
count++;
break;
}
if(sum > N)
break;
}
} printf("%d\n",count);
} return 0;
}

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】的更多相关文章

  1. How many prime numbers(求素数个数)

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

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

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

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

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

  5. JD 题目1040:Prime Number (筛法求素数)

    OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...

  6. Sum of Consecutive Prime Numbers(素数打表+尺取)

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

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

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

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

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

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

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

随机推荐

  1. JavaWeb学习笔记:Tomcat

    Tomcat 开源的 Servlet 容器. 部署并启动 tomcat server. 解压 apache-tomcat-6.0.16.zip 到一个非中文文件夹下. 配置一个环境变量. java_h ...

  2. Pig源代码分析: 简析运行计划的生成

    摘要 本文通过跟代码的方式,分析从输入一批Pig-latin到输出物理运行计划(与launcher引擎有关,通常是MR运行计划.也能够是Spark RDD的运行算子)的总体流程. 不会详细涉及AST怎 ...

  3. [NowCoder]牛客网NOIP赛前集训营-提高组(第七场)

    链接 A.中国式家长2 模拟题,毫无坑点 #include<bits/stdc++.h> #define REP(i,a,b) for(int i(a);i<=(b);++i) #d ...

  4. js中event事件处理

    1. HTML事件  直接添加到HTML结构中 function show() { alert('hello'); } <body> <button id="btn&quo ...

  5. 使用网络TCP搭建一个简单文件下载器

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶服务器Server 三丶测试TCP server服务器 四丶客户端Client 五丶测试客户端向服务器下载 ...

  6. selenium+python自动化处理时间控件

    尝试编写12306网站查询余票信息的自动化脚本时,碰到日期选择的问题,此处做一下记录:

  7. oracle 日期

    http://blog.itpub.net/126211/viewspace-712986/

  8. .NET中StringBuilder用法实例分析

    string s1 = "33"; string s2 = "44"; string s3 = "55"; //需求是把s1 s2 s3拼接 ...

  9. 二叉树的递归插入【Java实现】

    C++中由于有指针的存在,可以让二叉树节点指针的指针作为插入函数的实参,在函数体内通过*操作实现对真实节点指针.节点左孩子指针.节点右孩子指针的改变,这样很容易使用递归将大树问题转化到小树问题.但在J ...

  10. 多线程在python中的使用 thread

    近期想学习研究一下python中使用多线程,来提高python在爬虫项目中的效率. 如今我们在网页上查询到在python中使用的多线程的使用大多数都是使用的threading模块,可是python中另 ...