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. windows10 彻底卸载 Docker 和 DockerNAT

    删除docker程序 记事本新建脚本文件 a.ps1,内容如下: $ErrorActionPreference = "SilentlyContinue" kill -force - ...

  2. Linux常用基本命令大全

    1 一般情况下,自己安装linux系统都会选择简易版,这里面少很多命令,所以需要安装其他的包        yum install openssh-clients   安装scp的软件包 2 把当前一 ...

  3. [COGS 0107][NOIP 2003] 传染病控制

    107. [NOIP2003] 传染病控制 ★★★   输入文件:epidemic.in   输出文件:epidemic.out   简单对比时间限制:1 s   内存限制:128 MB [问题背景] ...

  4. [BZOJ 1124][POI 2008] 枪战 Maf

    1124: [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 659  Solved: 259[Submit][Status ...

  5. 动画的分类:属性(几何)动画、内容(视频)动画:gpu vs cpu

    属性动画通过gpu根据属性来呈现: 内容动画通过cpu解码内容按照时间呈现给gpu: (或者gpu直接解码现实?)

  6. BZOJ2330:[SCOI2011]糖果(差分约束)

    Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的 ...

  7. MyBatis(2)-全局配置文件

    本文的代码是在MyBatis(1)-简单入门基础之上进行学习的,如有不懂请先看此博文MyBatis(1)-简单入门! 1)配置文件的安装 --->在联网的情况下,点击去下载http://myba ...

  8. sys,os加glob加os.path用法

    1   sys.path是python的搜索模块的路径集,是一个list 打印下大概长这样 C:\Python27\Lib\idlelib C:\Windows\system32\python27.z ...

  9. 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。

    这个题让我更加明白了类创建对象的实质 代码中用到:1.对象作形参;2.对象作返回值 以下用代码具体分析: class Point1{ public double x; public double y; ...

  10. CSS3中为什么要清除浮动以及三种清除浮动(float)的方法

    方法一:添加新的元素 .应用 clear:both .clear{ clear:both; height: 0; height: 0; overflow:hidden; } 方法二:父级div定义 o ...