POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
描述
http://poj.org/problem?id=2739
多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22737 | Accepted: 12432 |
Description
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
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
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_(尺取法+素数表)的更多相关文章
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 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 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- poj_2739 尺取法
题目大意 给定一个数字N,N可能由1个或多个连续的素数求和得到,比如41 = 2+3+5+7+11+13, 41 = 11+13+17, 41 = 41.求出对于N,所有可能的组合形式. 题目分析 先 ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- POJ:3061-Subsequence(尺取法模板详解)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...
随机推荐
- IrisSkin4控件使用方法
参考如下: 1. 将IrisSkin4.dll动态文件导入当前项目引用中.具体操作为:解决方案资源管理器->当前项目->引用->右键->添加引用,找到IrisSkin4.dll ...
- win10 Administrator
和Win7/Win8.1一样,Win10的管理员账户Administrator是默认隐藏和关闭的,因为该账户权限极高,被不法分子利用后存在极大风险.但如果你想在某些特殊情况下使用该账户,就需要手动将其 ...
- powerbulider9.0在数据窗口中实现滚动到新添加行
powerbuilder9.0对数据窗口进行增加行操作,然后实现滚动到指定行时,应先滚动到指定行dw_1.scrolltorow( row),然后设置新添加的行为当前行dw_1.setrow( row ...
- 百度地图API调用实例之地址标注与位置显示
之前弄了个谷歌地图API标注的调用实例,后来要求改成百度地图. 感谢主,通过网上资料(百度地图API,百度地图API详解之地图标注)收集及研究, 终于把百度地图标注和显示功能实现出来了,具体实现方法如 ...
- Java——类比较器
1.Product类 public class Product { private int pid; private String name; private double price; public ...
- Eclipse相关
JDK版本更换相关: 启动eclipse会报错:根据报错信息后面提示的eclipse配置信息,我将配置中的c:/xx/javaw.exe给移除了.并在eclipse.ini中配置了-vm d:/Jav ...
- jQuery弹出层_点击自身以外地方关闭弹出层
<html> <style> .hide{display:none;} </style> <script type="text/javascript ...
- sass进阶篇总结一
一.@if 指令: @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还 ...
- umount 卸载的时候,提示busy!
mount /dev/sdb /mnt/disk umount -l /mnt/disk[有busy的问题可以加上l项] 1. 查询当前谁在使用device,fuser /mnt/temp,查询结果是 ...
- [BUGFIX]__import_pywin32_system_module__
import_pywin32_system_module 修复方法: 编辑 X:/Python27/Lib/site-packages/win32/lib/pywintypes.py 第114行 if ...