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. UT源码 005

    NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. 要求 ...

  2. JDBC(下)

    1. 预编译sql处理(防止sql注入) -- 创建数据库 CREATE DATABASE jdbc_demo DEFAULT CHARACTER SET utf8;i -- 创建表 USE jdbc ...

  3. 使用Func<>和Action简化委托

    /// <summary> /// 入口 /// </summary> public void Run() { TestDelegate t = test; t(); Acti ...

  4. Git远程仓库(二)

    昨天讲了Git安装使用和基本的命令,今天我说一下如何通过Git将本地管理的仓库添加到远程库 首先我们需要在www.github.com(如果打不开网页,请注意科学上网)上注册一个免费的账号,邮箱验证完 ...

  5. Sublime安装Package Control插件

    一.简易安装 打开Sublime text的console.打开console的快捷时ctrl+,或者在菜单栏点击View->Show Sonsole`.打开后将下面的代码复制到console中 ...

  6. Webpack 速成

    前言 如果你已经对Webpack精通了或者至少一直在工作中使用它,请关闭当前浏览器标签,无视这篇文章. 这篇文章本意是写给我自己看的,作为一篇Cookbook供快速查询和上手用.原因是虽然工作中会涉及 ...

  7. Java 对象序列化和反序列化

         之前的文章中我们介绍过有关字节流字符流的使用,当时我们对于将一个对象输出到流中的操作,使用DataOutputStream流将该对象中的每个属性值逐个输出到流中,读出时相反.在我们看来这种行 ...

  8. Spring execution表达式

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) th ...

  9. 性能测试分享:Jmeter多机协作原理

    性能测试分享:Jmeter多机协作原理   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90 ...

  10. 【Android】基于TCP协议的网络通信

    1.使用ServerSocket 创建TCP服务器端: ServerSocket server; try { server = new ServerSocket(8000); while (true) ...