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

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 题意:某些数可以是连续质数的和,那么给定数a,求a的分解有几种可能。
思路:先可以用埃氏筛选找出连续的质数,之后尺取法找可能的情况,值得注意的是两组可能的连续质数序列存在元素重叠的情况,所以每找到一组,尺取法的头部和尾部统一更新为上一种情况的尾部再加1,继续查找下一种情况。。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int N_MAX = + ;
int prime[N_MAX],res;
bool is_prime[N_MAX+],what;
int sieve(int n) {
int p = ;
for (int i = ; i <= n; i++)is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = i*i; j <= n; j += i)is_prime[j] = false;
}
}
return p;
}
int main() {
int n=sieve(N_MAX),a;
while (scanf("%d",&a)&&a) {
res = ,what = ;
int l= , r= , sum = ;
int bound=upper_bound(prime, prime + n, a)-prime;
while () {////////
for (;;) {////
while (r < bound&&sum < a) {
sum += prime[r++];
}
if (sum == a) {
res++;
what = ;
break;
}
if (sum < a) {//找不到了,终止搜索;
what = ;
break;
}
sum -= prime[l++];
}////
if (what) { l++; r = l; sum = ; }
else break;
}////////
printf("%d\n", res);
}
return ;
}

poj 2379 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 Total Submissions: 20050 ...

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

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

  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( *【素数存表】+暴力枚举 )

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

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

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

  8. poj 2739 Sum of Consecutive Prime Numbers 小结

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

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

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

随机推荐

  1. html页面简单访问限制

    PS:突然发现博客园有密码保护功能,已经可以满足基本需求了.博客园还能备份自己的所有数据,做到了数据归用户所有,平台只是展示,真是良心网站,大赞. 想要通过一个站点放一些东西给一些人看,但是又不想让所 ...

  2. iPhone如何设置自定义铃声?无需连接电脑,轻松几步就搞定!

    转载自: https://baijiahao.baidu.com/s?id=1594988016778457969&wfr=spider&for=pc 受够了iPhone自带的千篇一律 ...

  3. pandas处理较大数据量级的方法 - chunk,hdf,pkl

    前情提要: 工作原因需要处理一批约30G左右的CSV数据,数据量级不需要hadoop的使用,同时由于办公的本本内存较低的缘故,需要解读取数据时内存不足的原因. 操作流程: 方法与方式:首先是读取数据, ...

  4. ubuntu 16.04下如何打造 sublime python编程环境

    一.安装python3     ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...

  5. 汇编语言 Part 1——简介、基本语法、内存分段与内存地址

    简介 什么是汇编语言? 汇编语言是一种低级的编程语言,在程序的语句和体系结构的机器代码指令之间有很强的对应关系. 每种汇编语言都特定于特定的计算机体系结构,但需要解释或编译.汇编语言也可以称为符号机器 ...

  6. oracle如何保证读一致性 第二弹

    Oracle之数据库一致性读的原理 在Oracle数据库中,undo主要有三大作用:提供一致性读(Consistent Read).回滚事务(Rollback Transaction)以及实例恢复(I ...

  7. 记一次虚拟机无法挂载volume的怪异问题排查

    故障现象 使用nova volume-attach <server> <volume>命令挂载卷,命令没有返回错误,但是查看虚拟机状态,卷并没有挂载上. 故障原因 疑似虚拟机长 ...

  8. 有限状态机(FSM)的设计与实现

    有限状态机(FSM)是表示有限个状态及在这些状态之间的转移和动作等行为的数学模型,在计算机领域有着广泛的应用.通常FSM包含几个要素:状态的管理.状态的监控.状态的触发.状态触发后引发的动作.本文主要 ...

  9. 探究灰度测试(A/B Testing)

    一段小插曲 前段时间产品改版,产品经理为了改进用户体验,就决定改版用户的注册流程页面,但又怕身份证注册验证接口不稳定(第三方的身份证校验). 于是产品经理就让我通过随机概率去控制注册流程,让一部分用户 ...

  10. hexo博客发布注意事项

    最近把hexo博客内容写完了,就发布到github上面去,结果就出现各种一些小问题. 1.发布之后,hexo博客的css与js无法访问. 原因:没有配置正确的url路径.(配置文件_config.ym ...