POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门
Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536K
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
解题思路
题目大意:问有几种方式,把一个数分成几个连续的素数的和。
跑一遍埃氏筛选法,筛选出素数,然后尺取法的思路不断推进。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 10005;
int prime[MAX];
bool is_prime[MAX];
int main()
{
int p = 0;
memset(is_prime,true,sizeof(is_prime));
is_prime[0] = is_prime[1] = false;
for (int i = 2;i <= MAX;i++)
{
if (is_prime[i])
{
prime[p++] = i;
for (int j = 2*i;j <= MAX;j += i)
{
is_prime[j] = false;
}
}
}
int N;
while (~scanf("%d",&N) && N)
{
bool flag = false;
int cnt = 0,s = 0,t = 0,sum = 0;
for (;;)
{
if (prime[t] >= N) break;
while (sum < N)
{
sum += prime[t++];
if (sum == N)
{
flag = true;
}
}
if (flag)
{
cnt++;
flag = false;
}
sum -= prime[s++];
if (sum == N && prime[t] < N)
{
cnt++;
sum -= prime[s++];
}
}
if (is_prime[N])printf("%d\n",cnt+1);
else if (cnt)printf("%d\n",cnt);
else printf("0\n");
}
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 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 ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
随机推荐
- 用python代码做configure文件
在lua中,我一直用lua作为config文件,或者承载数据的文件 - 好处是lua本身就很好阅读,然后无需额外写解析的代码,还支持在configure文件中读环境变量,条件判断等,方便又强大! (在 ...
- .Net分布式异常报警系统-简介
系统简介 分布式异常报警系统就是收集系统运行过程中产生的未处理异常,检查系统运行的状态,并将异常信息统一发送到服务端,由服务端将信息通知到相关的责任人. 问题 我们在项目开发中可能遇到以下几个问题: ...
- SQL Server Data Tools – Business Intelligence for Visual Studio 2012安装时提示“The CPU architecture....”的解决方法
SQL Server Data Tools – Business Intelligence for Visual Studio 2012,一个很强大的工具,下载地址:http://www.micros ...
- 发短信的简单实现——C#版
为了验证操作人的身份,界面中通常会有获取验证码的功能.及点击获取验证码就会往你输入的手机号里面发送一条短信进行验证. 最近公司给我的任务中也包含这个功能,那么接下来就让我讲解下. ---------- ...
- java泛型中的对象
import java.util.HashMap; class Key { String s; Key(String s) { this.s = new String(s); } @Override ...
- Ubuntu下安装支付宝安全控件
在淘宝购物时,安装支付宝安全控件.下载了一个文件.tar.gz(非常小的一个文件). tar -zxvf 解压之,只有一个aliedit.sh文件,运行这个文件就安装成功了,重启firefox就可以用 ...
- Ubuntu Terminal Shortcut
Not all of the shortcuts are useful.Only remeber the most useful. 移动类Ctrl + a - Jump to the start o ...
- android开发------初识Activity
之前我们简单说过,Activity实际上是一个窗体,用来存放我们的程序外观. 我们先来创建一个空的Activity,不加载任何layout.要做的是,定义自己的类,继承android的Activity ...
- canvas缓动
通过不断地将与目标的距离和系数相乘来让物体实现远快近缓的运动. 如图所示可以做出缓动效果,具体代码如下 var canvas = document.getElementById("canva ...
- Android 用代码设置Shape,corners,Gradient
网上查找资料 记录学习 int strokeWidth = 5; // 3dp 边框宽度 int roundRadius = 15; // 8dp 圆角半径 int strokeColor = Col ...