POJ:2739-Sum of Consecutive Prime Numbers(尺取)
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
解题心得:
- 题意就是一个数可以由多个连续的素数相加得到,现在给你一个素数n,问你有几种由连续素数相加的方案。
 - 先素数筛选,把所有的素数放在一个数组里面,然后用尺取法在里面跑就行了。
 
#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(尺取)的更多相关文章
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
		
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
 - POJ.2739 Sum of Consecutive Prime Numbers(水)
		
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
 - POJ 2739 Sum of Consecutive Prime Numbers(素数)
		
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
 - POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
		
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
 - POJ 2739. Sum of Consecutive Prime Numbers
		
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
 - poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
		
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
 - POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
		
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
 - POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
		
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
 - poj 2739 Sum of Consecutive Prime Numbers 小结
		
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
 
随机推荐
- Cookie存储大小、个数限制
			
一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...
 - .Net CIL
			
MachineCode->Assembly->CIL(.Net) or SpecialMachineCode(JVM)->Pogramming code CIL Instructio ...
 - HTML5 笔记之 HTML5 的常见用法介绍
			
阅读目录 介绍 网页标题.文章标题.文章段落 介绍 字体大小.字体颜色.字体类型.字体位置.背景颜色 介绍 插入图片 介绍 网页内的超链接.网页间的超链接 介绍 有序列表.无序列表 介绍 表格制作 介 ...
 - C#多线程Thread
			
在项目中经常用到线程Thread,先做个简单记录,后面再完善下,方便以后参考.本人技术有限,如有不同见解之处,欢迎博友批评指正. 执行的线程Thread分无参数的,一个参数,多个参数的.直接看代码吧. ...
 - MySQL入门很简单:  12  MYSQL 用户管理
			
1. 权限表 安装MySQL会自动安装一个名为mysql的数据库,存储权限表: user表, db表,host表,table_priv表,columns_priv表,proc_priv表等. 1)us ...
 - RPC电源监控总结
			
首先说一下监控机箱的监控原理. 设备的信息传输是通过tcp或者udp传输十六进制的数然后进行解析,传输数据. 如图: 设备反馈信息也是返回来的十六机制,然后按照对应的位置进将数据解析成二进制,用二进制 ...
 - Android 中间白色渐变到看不见的线的Drawable
			
用gradient <gradient android:startColor="#00ffffff" android:centerColor="#ffffff&qu ...
 - mongodb在C#的连接以及curd写法
			
连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...
 - HashMap扩容
			
前言:当您在读该文章的时候,我认为您已经知道HashMap的底层实现原理,如果您还不清楚HashMap是如何实现的,请先去了解,再回来看本文章. 1.HashMap什么时候扩容? HashMap的容量 ...
 - 深搜,四色定理,(POJ1129)
			
题目链接:http://poj.org/problem?id=1129 解题报告: #include<iostream> #include<cstdio> #include&l ...