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 ...
随机推荐
- onActivityResult不被执行的问题。
1.首先,返回的Activity必须使用startActivityForResult启动. 2.其次,在返回的Activity中必须保证setResult方法在finish方法之前执行,否则onAct ...
- 访问 HTML中元素的方法
http://www.w3school.com.cn/jsref/index.asp 1.document.getElementbyId("id1"),Html中,名称是id1 ...
- Java Lambda简明教程(一)
Lambda表达式背景 许多热门的编程语言如今都有一个叫做lambda或者闭包的语言特性,包括比较经典的函数式编程语言Lisp,Scheme,也有稍微年轻的语言比如JavaScript,Python, ...
- 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题
题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...
- spark-shell - 将结果保存成一个文件
sqlContext.sql(""" SELECT user_no,cust_id,oper_code FROM cui.operation_data_an ...
- tp_shop解读1
由于想弄一个商城,因此研究了一下tp_shop,这个据说能完成几乎所有的功能. 考虑到原有的例子过于复杂,因此把所有相关的数据都删除了,结果上来就出错了,查了两天,大致弄清楚了状况. 关于错误的原因 ...
- cakephp recursive -1,0,1,2 速查
-1 : model本身 0 :model本身 + belongTo + hasOne 1 :model本身 + belongTo + hasOne + hasMany 2: :model ...
- firefox下对ajax的onreadystatechange的支持情况分析及解决
一.问题: var xmlHttp; function savecarttodata(){ createXMLHttpRequest(); var rndcode = new Date().getTi ...
- javascript进阶——面向对象特性
面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...
- CentOS系统安全配置
http://down.51cto.com/data/318797 http://www.centos.bz/2011/07/centos-system-security-configure/ htt ...