poj 2739 Sum of Consecutive Prime Numbers 尺取法
| Time Limit: 1000MS | Memory Limit: 65536K |
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
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 尺取法的更多相关文章
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
随机推荐
- 沉淀,再出发:python爬虫的再次思考
沉淀,再出发:python爬虫的再次思考 一.前言 之前笔者就写过python爬虫的相关文档,不过当时因为知识所限,理解和掌握的东西都非常的少,并且使用更多的是python2.x的版本的功能,现 ...
- December 28th 2016 Week 53rd Wednesday
Knowledge is a treasure, but practice is the key to it. 知识是珍宝,而实践是获取她的钥匙. I know a lot, but what I r ...
- Linux命令--压缩解压(简化版)
Linux tar.gz.tar.bz2.zip 等解压缩.压缩命令详解(简化版) Linux 常用的压缩与解压缩命令有:tar.gzip.gunzip.bzip2.bunzip2.compress ...
- Python中readline()函数 去除换行符
从Python中readline()函数读取的一行内容中含有换行符\n,很多时候我们需要处理不含有换行符的字符串,此时就要去掉换行符\n. 方法是使用strip()函数. 例子如下: f = open ...
- linux性能系列--cpu
一.先看看什么是CPU? 回答:中央处理器(Central Processing Unit)的缩写,即CPU,CPU是电脑中的核心配件,只有火柴盒那么大,几十张纸那么厚,但它却是一台计算机的运算核心和 ...
- CSS3动画中的位置设定问题
水平居中的不同方法实现: position: absolute; margin: auto; left:; right:; position: absolute; left:%; -webkit-tr ...
- 我的第一个 Servlet
简单记录一下我从头写一个 Servlet 的过程. 我安装的是 Tomcat 7 版本,在 Ubuntu 18.04 上运行,IDE 为 Intellij IDEA. 首先创建一个 Java Web ...
- BZOJ1058:[ZJOI2007]报表统计(Splay,堆)
Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工 作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个 ...
- [POI2006]TET-Tetris 3D
题目 二维线段树板子题啊 但是惊讶的发现我不会打标记 毕竟内层是线段树不能\(pushup\)也不能\(pushdown\) 于是考虑一下标记永久化 其实非常显然\(mx_i\)表示区间最大值,\(t ...
- urllib库基本使用
#导入urllib库 import urllib.request #打开网址 file=urllib.request.urlopen("http://www.sohu.com/", ...