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
题意:输入一个数字(<=1e5)求该数可由几种在素数表中连续的素数之和组成
思路:用尺取法,注意退出循环的情况
 #include <iostream>
#include <cstdio>
using namespace std;
#define N 10010 int prime[N];//素数表 int quickmod(int a,int b,int c)//快速幂模
{
int ans=; a=a%c; while (b)
{
if (b&)
{
ans=ans*a%c;
}
a=a*a%c;
b>>=;
} return ans;
} bool miller(int n)//米勒求素数法
{
int i,s[]={,,,,}; for (i=;i<;i++)
{
if (n==s[i])
{
return true;
} if (quickmod(s[i],n-,n)!=)
{
return false;
}
}
return true;
} void init()
{
int i,j; for (i=,j=;i<N;i++)//坑点:注意是i<N,而不是j<N
{
if (miller(i))
{
prime[j]=i;
j++;
}
}
} void test()
{
int i;
for (i=;i<N;i++)
{
printf("%6d",prime[i]);
}
} int main()
{
int n,l,r,ans,sum;//l为尺取法的左端点,r为右端点,ans为答案,sum为该段素数和 init();
// test(); while (scanf("%d",&n)&&n)
{
l=r=ans=;
sum=; for (;;)
{
while (sum<n&&prime[r+]<=n)//prime[r+1]<=n表示该数是可加的,意即右端点还可以继续右移
{
sum+=prime[++r];
} if (sum<n)//右端点无法继续右移,而左端点的右移只能使sum减小,意即sum数组无法再大于等于n,就可以退出循环
{
break;
} else if (sum>n)
{
sum-=prime[l++];
} else if (sum==n)
{
ans++;
sum=sum-prime[l];
l++;
}
} printf("%d\n",ans);
} return ;
}

poj 2739 Sum of Consecutive Prime Numbers 尺取法的更多相关文章

  1. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  2. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

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

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

  4. POJ 2739. Sum of Consecutive Prime Numbers

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

  5. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

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

  6. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

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

  7. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

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

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

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. php性能调优

    第一章  针对系统调用过多的优化 我这次的优化针对syscall调用过多的问题,所以使用strace跟踪apache进行分析. 1.  apache2ctl -X & 使用-X(debug)参 ...

  2. Redis数据的底层存储原理

    redis底层是用什么结构来存储数据的呢? 我们从源码上去理解就会容易的多:   redis底层是使用C语言来编写的,我们可以看到它的数据结构声明.一个 dict 有两个dictht,一个dictht ...

  3. MVC渲染文章内容的html标签转义

    文章详情页一般从数据库中取出文章内容,文章内容一般含有 等html标签,MVC中如果直接从模型输出文章内容,会把html标签转义变成<&gt等,这时候是要把转义后的标签变成html标签, ...

  4. yii2视频教材

    http://www.weixistyle.com/ http://www.imooc.com/learn/743

  5. Linux Notes | Linux常用命令行笔记

    [ show all running processes ] (1) ps -aux | less 'ps' means: Process Status The -a option tells ps ...

  6. LCG(linear congruential generator): 一种简单的随机数生成算法

    目录 LCG算法 python 实现 LCG算法 LCG(linear congruential generator)线性同余算法,是一个古老的产生随机数的算法.由以下参数组成: 参数 m a c X ...

  7. redis集群环境搭建的错误

    安装redis集群需要版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本. 需要安装ruby的环境. yum -y install ru ...

  8. nginx编译问题:make[1]: *** [/usr/local/pcre//Makefile] Error 127

    解决方法: 是由于nginx高版本的需要使用pcre原文件路径. 解压pcre-7.9.tar.gz 例如解压后位置在 /home/wang/pcre-7.9位置 使用nginx配置的时候 ./con ...

  9. 如何使用jquery.qrcode.js插件生成二维码

    1.首先需要准备 jquery.qrcode.js 和 jquery.js github地址:https://github.com/lrsjng/jquery-qrcode 官方文档地址:http:/ ...

  10. C++获取毫秒级时间戳

    #include<chrono>   auto timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::sy ...