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 ...
随机推荐
- css 笔记1
type="text/css"的作用是什么?它是CSS样式的标记.type->类型,这里是style的属性text/css ->文本/css,即css文本type=&q ...
- python 实现插入排序、冒泡排序、归并排序
def InsertSort(A): '''插入排序算法:传入一个list,对list中的数字进行排序''' print('插入排序前list元素顺序:',A) length=len(A) for i ...
- Mininet自定义网络拓扑
在Mininet上的网络拓扑有两种方式 第一种 用mininet自带的miniedit可视化工具,在mininet/mininet/examples/的目录下的一个miniedit.py,运行这个文件 ...
- linux性能系列--网络
一.为啥网络监控不好做? 回答:网络是所有子系统中最难监控的了.首先是由于网络是抽象的,更重要的是许多影响网络的因素并不在我们的控制范围之内.这些因素包括,延迟.冲突.阻塞等 等.由于网络监控中, ...
- Python类的继承(进阶5)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411918.html 本文出自:[Edwin博客园] Python类的继承(进阶5) 1. python中什 ...
- JavaScript的事件的处理函数(鼠标,键盘,HTML)
事件处理函数概述 JavaScript 可以处理的事件类型为:鼠标事件.键盘事件.HTML 事件 所有的事件处理函数都会都有两个部分组成,on + 事件名称,例如 click 事件的事件处理函数就是: ...
- JavaScript的DOM_操作行内样式
一.检测浏览器是否支持css CSS 作为(X)HTML 的辅助,可以增强页面的显示效果.但不是每个浏览器都能支持最新的 CSS 能力.CSS 的能力和 DOM 级别密切相关,所以我们有必要检测当前浏 ...
- Input and Output-The input is all the sources of action for your app
Programs take input and produce output. The output is the result of doing something with the input. ...
- angularjs select下拉搜索框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php filesize() 方法返回的文件大小异常
问题描述:需要生成一个 700k 大小左右的文件,相关代码如下: <?php $handle = fopen('./split.log', 'a+'); $result = 0; while( ...