Sum of Consecutive Prime Numbers

http://poj.org/problem?id=2739

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28929   Accepted: 15525

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

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
#define PI acos(-1.0)
#define eps 1e-9
using namespace std;
int prime[];//存素数
bool vis[];//保证不做素数的倍数
void dabiao(int n){
int cnt = ;
memset(vis, false, sizeof(vis));//初始化
memset(prime, , sizeof(prime));
for(int i = ; i <= n; i++)
{
if(!vis[i])//不是目前找到的素数的倍数
prime[cnt++] = i;//找到素数~
for(int j = ; j<cnt && i*prime[j]<=n; j++)
{
vis[i*prime[j]] = true;//找到的素数的倍数不访问
if(i % prime[j] == ) break;//关键!!!!
}
}
} int main(){ dabiao();
int n;
while(~scanf("%d",&n)){
if(!n) break;
int L=,R=;
int ans=;
int sum=;
int pos=upper_bound(prime,prime+,n)-prime;
while(L<=R){ if(ans<=n&&R<pos){
ans+=prime[R++];
}
else if(ans>n||R==pos){
ans-=prime[L++];
}
if(ans==n){
sum++;
}
}
printf("%d\n",sum);
} }

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

  1. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

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

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

  4. 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 ...

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

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

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

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

  7. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

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

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

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

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

随机推荐

  1. js判断是android访问还是ios访问

    原文地址:http://blog.csdn.net/wy978651775/article/details/9014039 该博主也是转载的,但是没有标明出处. 判断原理: JavaScript是前端 ...

  2. Nginx Tengine ngx_http_upstream_check_module 健康功能检测使用

    该模块可以为Tengine提供主动式后端服务器健康检查的功能. 该模块在Tengine-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_ ...

  3. Ubuntu安装配置串口通讯工具minicom&&cutecom

    原帖地址:https://blog.csdn.net/gatieme/article/details/45310493 2017-04-07更新 发现新的工具gtkterm全名叫serial port ...

  4. 使用eclipse在linux下开发C/C++

    一直在Linux下开发,苦于没有IDE,一般都是自己编写Makefile,然后在windows下用文本编辑器ftp打开文件编辑,然后在linux下完成编译.调试代码也只能是命令行用gdb进行调试,相当 ...

  5. CSS 3 学习笔记

    css 3 学习笔记 文本: word-wrap : normal | break-word取值:normal:    控制连续文本换行.break-word:    内容将在边界内换行.如果需要,词 ...

  6. Java的this和super总结

    内容: 1.this和super作用 2.继承关系图 1.this和super作用 this和super的作用: this:区分本类中的成员变量和局部变量同名的情况,代指本类 super:区分子类中的 ...

  7. HTML|CSS之HTML常用标签

    知识内容: 1.标签 2.head内标签 3.body内常用标签 注:本人使用的HTML为HTML5 一.标签 1.标签格式 标签的语法: <标签名 属性1=“属性值1” 属性2=“属性值2”… ...

  8. vi规范

    pep8规范 # vi规范## , 后空一格# 函数和其他代码空两行

  9. spring quartz 任务注入spring service

    SchedulerFactoryBean+AdaptableJobFactory+QuartzJobBean package schedule.quartz5; import org.quartz.S ...

  10. Python之实例对象的增删改查

    #实例对象的增删改查p1 = ChinesePeople('wangyue')#print (p1.__dict__) #查看实例对象的属性#print (p1.name)(p1.play_ball( ...