poj 2379 Sum of Consecutive Prime Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24498 | Accepted: 13326 |
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
separate line. The integers are between 2 and 10 000, inclusive. The end of the
input is indicated by a zero.
Output
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 题意:某些数可以是连续质数的和,那么给定数a,求a的分解有几种可能。
思路:先可以用埃氏筛选找出连续的质数,之后尺取法找可能的情况,值得注意的是两组可能的连续质数序列存在元素重叠的情况,所以每找到一组,尺取法的头部和尾部统一更新为上一种情况的尾部再加1,继续查找下一种情况。。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int N_MAX = + ;
int prime[N_MAX],res;
bool is_prime[N_MAX+],what;
int sieve(int n) {
int p = ;
for (int i = ; i <= n; i++)is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = i*i; j <= n; j += i)is_prime[j] = false;
}
}
return p;
}
int main() {
int n=sieve(N_MAX),a;
while (scanf("%d",&a)&&a) {
res = ,what = ;
int l= , r= , sum = ;
int bound=upper_bound(prime, prime + n, a)-prime;
while () {////////
for (;;) {////
while (r < bound&&sum < a) {
sum += prime[r++];
}
if (sum == a) {
res++;
what = ;
break;
}
if (sum < a) {//找不到了,终止搜索;
what = ;
break;
}
sum -= prime[l++];
}////
if (what) { l++; r = l; sum = ; }
else break;
}////////
printf("%d\n", res);
}
return ;
}
poj 2379 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 Total Submissions: 20050 ...
- 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 素数 读题 难度: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 ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
随机推荐
- ASP.NET WebApi 路由配置
ASP.NET Web API路由是整个API的入口.我们访问某个资源就是通过路由映射找到对应资源的URL.通过URL来获取资源的. 对于ASP.NET Web API内部实现来讲,我们的请求最终将定 ...
- Tomcat详细安装配置
1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方网站下载,开源免费,而且带宽也足够.下载会很快. 这是两种不同的下载,一个是普通安装版本,一个是解压安装版本.使用起来是一样的,只是 ...
- iOS开发之WIFI,3G/4G两种网络同时使用技巧
最近遇到一个比较奇葩的需求:App与硬件通过WiFi LAN通信, 同时App需要与服务器通过3G/4G WAN通信,如下图: 众所周知,手机同时打开WiFi和3G时候,会优先走WiFi.这个该如何实 ...
- 用宝塔软件在linux上自动安装php环境
1.确保是纯净系统 确保是干净的操作系统,没有安装过其它环境带的Apache/Nginx/php/MySQL,否则安装不上 2.sudo进行安装 yum install -y wget &&a ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- k短路模板
https://acm.taifua.com/archives/jsk31445.html 链接: https://nanti.jisuanke.com/t/31445 #include <io ...
- Counting Cliques HDU - 5952 单向边dfs
题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...
- 排序 sort函数
sort函数见下表: 函数名 功能描述 sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 partial_sort 对给定区间所有元素部分排序 partia ...
- flask-mail(qq邮箱)
from flask_mail import Mail,Message app.config['MAIL_SERVER']='smtp.qq.com' app.config['MAIL_PORT'] ...
- Python虚拟机之异常控制流(四)
Python虚拟机中的异常控制流 先前,我们分别介绍了Python虚拟机之if控制流(一).Python虚拟机之for循环控制流(二)和Python虚拟机之while循环控制结构(三).这一章,我们来 ...