Sum of Consecutive Prime Numbers

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 27853 Accepted: 14968

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


解题心得:

  1. 题意就是一个数可以由多个连续的素数相加得到,现在给你一个素数n,问你有几种由连续素数相加的方案。
  2. 先素数筛选,把所有的素数放在一个数组里面,然后用尺取法在里面跑就行了。

#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e4+100;
vector <int> ve;
int n;
bool vis[maxn]; void pre_deal() {
for(int i=2;i<=maxn;i++) {
if(vis[i])
continue;
ve.push_back(i);
for(int j=i*2;j<=maxn;j+=i) {
vis[j] = true;
}
}
} int cal_num() {
int l = 0,r = 0,sum = 0,cnt = 0;
while(r < ve.size()) {
while(sum < n && r < ve.size()) {
sum += ve[r];
r++;
}
if(sum < n)
break;
if(sum == n)
cnt++;
sum -= ve[l];
l++;
}
return cnt;
} int main() {
pre_deal();
while(scanf("%d",&n) && n) {
printf("%d\n",cal_num());
}
return 0;
}

POJ:2739-Sum of Consecutive Prime Numbers(尺取)的更多相关文章

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

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

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

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

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

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

  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

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

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

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

  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【素数打表】

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

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

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

随机推荐

  1. 将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载

    public static Stream CreateZip(List<string> listPath, int level = 5) { MemoryStream mstream = ...

  2. HashWithIndifferentAccess

    The params method returns the parameters passed to the action, such as those fromthe form or query p ...

  3. Java—运算符和流程控制语句

    运算符优先级: siwtch语句注意事项: switch 后面小括号中表达式的值必须是整型或字符型 case 后面的值可以是常量数值,如 1.2:也可以是一个常量表达式,如 2+2 :但不能是变量或带 ...

  4. Visual Studio 编辑器打开项目后,一直提醒Vs在忙,解决方法

    今天打开VS2015后,因为这个解决中有很项目,突然就一直现在加载中,点击VS提示在忙,怎么破那?请往下看 第一种方法 1.关闭VS: 2.去C:\Users\<your users name& ...

  5. PDO数据库类——对query()和exec()的异常监听

    PDO异常类中,query()和exec()方法中执行失败时,默认情况下,我们是无法知道,具体执行失败的原因. 那如果我们想要监听异常的话,肿么整呢? 只要使用setAttribute()方法,即可监 ...

  6. 如何处理用代码创建SD Sales order时遇到的错误消息KI 180

    错误消息KI 180:You must enter a company code for transaction Create sales document 代码: REPORT zcreate_so ...

  7. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  8. mongodb在C#的连接以及curd写法

    连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...

  9. url获取MVC域,action,controller的方法

    域:filterContext.RequestContext.RouteData.DataTokens["area"] 控制器:filterContext.RequestConte ...

  10. Poj(1125),Floyd,

    题目链接:http://poj.org/problem?id=1125 多源点最短路中的,最长路的,最短路. 看到这里就懵逼了,解释一下,找到一个源点,使得路最短,(遍历源点),路最短怎么求呢? 就是 ...