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
题意:给你一个数,要求找出它能用连续素数相加而成的个数
题解:一看就知道要先来一个素数筛啦。然后用另一个数组保存2到10000的尺取结果,输入后就能直接输出了,刚开始还担心会不会TLE@-@,结果居然只花了188ms
果然还是打表大法好啊
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int p[maxn],ans[maxn];
bool isprime[N]; void getprime()
{
int k=;
for(int i=;i<N;i++)isprime[i]=;
isprime[]=isprime[]=;
for(int i=;i<N;i++)
{
if(isprime[i])
{
p[k++]=i;
for(int j=*i;j<N;j+=i)
isprime[j]=;
}
}
}
int solve(int x)//对x进行尺取
{
int s=,t=,sum=,ans=;
while(t<){
while(sum<x&&t<){
sum+=p[t];
t++;
}
if(sum<x)break;
if(sum==x)ans++;
sum-=p[s];
s++;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
getprime();
for(int i=;i<=;i++)ans[i]=solve(i);
int n;
while(cin>>n,n){
cout<<ans[n]<<endl;
}
return ;
}

poj2739尺取法+素数筛的更多相关文章

  1. poj2739(尺取法+质数筛)

    题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...

  2. POJ2739(尺取法)

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23931 ...

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

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

  4. 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers

    给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点               ...

  5. POJ 尺取法

    poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...

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

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

  7. POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)

    描述 http://poj.org/problem?id=2739 多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n. Sum of Consecutive Prime Numb ...

  8. poj_2739 尺取法

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

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

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

随机推荐

  1. DARK的锁链

    Description 无向图中有N个节点和两类边,一类边被称为主要边,而另一类被称为附加边.Dark有N – 1条主要边,并且Dark的任意两个节点之间都存在一条只由主要边构成的路径.另外,Dark ...

  2. C++枚举类型详解

    原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5171110.html  一.枚举类型的定义 enum 类型名 {枚举值表}: 类型名是变量名,指定 ...

  3. 一个请求中,ADF、JSF究竟做了哪些工作

    在Oracle ADF开发中,一个请求发生后,经过ADF处理后,我们可以很快得到响应页面,但在请求过程中ADF框架在背后究竟做了什么东西呢?今天让我们一起来了解下,ADF.JSF是基于组件模型的,不同 ...

  4. 如何使用ArcGIS发布LiDAR 点云

    LiDAR--Light Detection And Ranging,即激光探测与测量技术. 下面将介绍如何使用ARCGIS来发布LiDAR的成果点云数据. LiDAR的点云数据一般格式为LAS.在A ...

  5. javascript与jQuery选项卡效果

    HTML结构: <!doctype html><html><head><meta charset="utf-8"><title ...

  6. java开发中的链式思维 —— 设计一个链式过滤器

    概述 最近在弄阿里云的sls日志服务,该服务提供了一个搜索接口,可根据各种运算.逻辑等表达式搜出想要的内容.具体语法可见https://help.aliyun.com/document_detail/ ...

  7. 【JavaWeb】Session(转)

    Session是服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资源时,可以把各自 ...

  8. Excel公式-求最低价网站名字

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  9. 转接口IC NCS8807:LVDS转MINI LVDS芯片

    LVDS 4K TCON w/ Scaler1 General Description    NCS8807 is an LVDS 4K TCON with advanced scaling func ...

  10. JAVA加密算法系列-AES

    package ***; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; ...