描述


http://poj.org/problem?id=2739

多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n.

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

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

Source

分析


尺取法.

打个素数表,对于每一个n,尺取之.

换了种尺取法的写法= =.

 #include<cstdio>

 const int maxn=;
int p,n;
int prime[maxn];
bool is_prime[maxn]; void get_prime()
{
for(int i=;i<maxn;i++) is_prime[i]=true;
is_prime[]=is_prime[]=false;
p=;
for(int i=;i<maxn;i++)
{
if(is_prime[i])
{
prime[++p]=i;
for(int j=*i;j<maxn;j+=i) is_prime[j]=false;
}
}
} void solve()
{
int l=,r=,sum=,ans=;
while(l<=p&&r<=p&&prime[l]<=n&&prime[r]<=n)
{
if(sum==n) { ans++; sum-=prime[l++]; }
else if(sum>n) sum-=prime[l++];
else sum+=prime[++r];
}
printf("%d\n",ans);
} void init()
{
get_prime();
while(scanf("%d",&n)==&&n!=) solve();
} int main()
{
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}

POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)的更多相关文章

  1. poj2739尺取法+素数筛

    Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...

  2. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  3. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

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

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

  5. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

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

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

  7. poj_2739 尺取法

    题目大意 给定一个数字N,N可能由1个或多个连续的素数求和得到,比如41 = 2+3+5+7+11+13, 41 = 11+13+17, 41 = 41.求出对于N,所有可能的组合形式. 题目分析 先 ...

  8. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

  9. POJ:3061-Subsequence(尺取法模板详解)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...

随机推荐

  1. Visual C++ 打印编程技术-编程基础-映射模式

    映射模式: Visual C++ 中采用的坐标映射方式使得用户图形坐标和输出设别的像素完全一致. eg:当屏幕的像素大小为800X600时,每英寸包含屏幕像素为96,打印机则需要几倍的点数才能达到同样 ...

  2. JS中(function(){xxx})(); 这种写法是什么意思?

    自执行匿名函数: 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命 ...

  3. 工作中的问题解决 -- (win2003 asp.net) Session和带页面回传的方法无法正常使用解决方案

    公司BP&IT项目组.从上上个月成立开始开发BP&IT软件.这个月开始测试我悲剧的发现他尽然不支持我电脑上的IE11.半个多月还没解决 我们先来分析下原因首页 登陆页面正常浏览 htt ...

  4. win7音量控制图标不见了怎么办啦?

    1.打开程序管理器(ctrl+alt+delete)2.在进程那里找到"explorer.exe",然后按结束进程(此时工具栏会消失)3.然后在文件(程序管理器左上角),点击&qu ...

  5. LA 3177 Beijing Guards(二分法 贪心)

    Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...

  6. Codevs 1172 Hankson 的趣味题 2009年NOIP全国联赛提高组

    1172 Hankson 的趣味题 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Hanks 博 ...

  7. (转 部分修改) IOS 手势密码(简单版)

    // // Created by wangtouwang on 15/4/7. // Copyright (c) 2015年 wangtouwang. All rights reserved. // ...

  8. 深度优化LNMP之PHP (转)

    深度优化LNMP之PHP   PHP缓存加速介绍   1.操作码介绍及缓存原理     当客户端请求一个php程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate ...

  9. 【转】Nginx 服务器安装及配置文件详解

    1. 安装nginx 1.1 选择稳定版本 我们编译安装nginx来定制自己的模块,机器CentOS 6.2 x86_64.首先安装缺少的依赖包: # yum -y install gcc gcc-c ...

  10. 《sed的流艺术之二》-linux命令五分钟系列之二十二

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...