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. Bootstrap 下拉菜单(dropdown)插件

    使用下拉菜单的插件,您可以向任何组件(比如:导航栏,标签页,胶囊式导航,按钮)添加下拉菜单 用法 您可以切换下拉菜单(dropdown)插件隐藏内容 1.通过data属性,向链接或按钮添加data-t ...

  2. 约束Constraints

    1.setNeedsUpdateConstraints:当想要调整子视图布局时,在主线程调用该方法标记constraint需要在未来的某个点更新(该方法不会立刻强制刷新constraint,而是等待下 ...

  3. 【数学 思维题】HDU4473Exam

    过程很美妙啊 Problem Description Rikka is a high school girl suffering seriously from Chūnibyō (the age of ...

  4. 解决mysql出现的问题#1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i

    最近在学flask, 在访问主页时,一直出现1055错误,在网上找的解决方法是删除ONLY_FULL_GROUP_BY,当时是删除了,但是退出在进行select @@sql_mode时,仍出现ONLY ...

  5. kali添加更新源

    /etc/apt/sources.list 具体方法参考: http://blog.csdn.net/gmnet/article/details/14471835 http://blog.sina.c ...

  6. Beyond Compare 4 30天试用期后,破解方法

    Beyond Compare 4 30天试用期后,破解方法. 方法一:在安装目录下找到文件BCUnrar.dll,比如:D:\software\Beyond Compare 4,重命名该文件即可. 重 ...

  7. k短路模板

    https://acm.taifua.com/archives/jsk31445.html 链接: https://nanti.jisuanke.com/t/31445 #include <io ...

  8. WPF触控程序的开发(一)——有用的资源

    迟来的一篇博文,每次都要撞到月末,这个月实在太忙了,除了在公司上班,还接了个单子,用wpf做一个触屏软件,类似iphone的相册功能.先说搭建开发环境吧,我是不可能去买个平板来的,再说基于win7的程 ...

  9. dedecms 标签

    article文章页标签 文档工具:http://tools.dedecms.com/dedetag_maker/article.html {dede:field.title/} 文章标题 {dede ...

  10. Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)

    题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...