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. 前端防御XSS

    下面是前端过滤XSS的代码,取自于百度FEX前端团队的Ueditor在线编辑器: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function xssCheck(str,r ...

  2. Android 接入支付宝支付实现

    接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...

  3. typedef struct 与 struct

    学c++之前最好先学c.特别要说的是,一些虽然冠名为c++的项目的文件中却大部分都是c的代码. 比如我们这个例子: 在c语言中,定义一个结构体和其实适合c++中有区别的.比如我们有如下的代码: str ...

  4. ReactNative-JS 调用原生方法实例代码(转载)

    第一步首先创建ReactNative 模块类继承ReactContextBaseJavaModule package com.mixture;   import android.content.Con ...

  5. CentOS6.8上安装epel

    实验环境: [root@bogon zhi]# uname -a Linux bogon -.el6.i686 # SMP Fri Nov :: UTC i686 i686 i386 GNU/Linu ...

  6. S7-1500 读取V90/S120/S210/G120的常用驱动参数

    S7-1500 读取V90/S120的常用驱动参数 此程序已更新,可以下载例子程序 https://files.cnblogs.com/files/lion-zheng/PLC_async_drive ...

  7. CRM WebClient UI里的文件是如何上传到Netweaver后台的

    使用Chrome开发者工具调试CRM WebClient UI里附件上传的功能: 从本地选择一个文件,断点触发: 前端取得用户选中上传的文件名: Jerry.txt 点Attach按钮后,触发ABAP ...

  8. POJ-1990 MooFest---两个树状数组

    题目链接: https://vjudge.net/problem/POJ-1990 题目大意: 一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛 ...

  9. 木棒,POJ(1011)

    题目链接:http://poj.org/problem?id=1011 解题报告: #include <cstdio> #include <cstring> #include ...

  10. python 删除空白

    Python能够找出字符串开头和末尾多余的空白.要确保字符串末尾没有空白,可使用方法rstrip() . >>> favorite_language = 'python ' > ...