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 ...
随机推荐
- Java的日期类和日期格式化类
日期类: Date date = new Date(); // 获取当前的系统时间 2 System.out.println("年份:"+ date.getYear()); Cal ...
- java基础—注解annotation
一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts ...
- ovx openVirtex的阅读文档
由于flowvisor只有4个版本, 最新更新都是2013年的, 跟底层ovs版本不跟进, 最近斯坦福post一个 ovx, 猜测是flowvisor的加强版, 所以看一下文档说明 文档详见http: ...
- Mybatis学习记录(1)
1.Mybatis介绍 Mybatis是apache的一个开源项目iBatis,Mybatis是一个优秀的持久层框架,他对jdbc的操作数据库的过程进行封装,使开发者只需要关注sql本身,不需 ...
- 洛谷 P1835 素数密度
https://www.luogu.org/problemnew/show/P1835 对于40%,对每个数进行最大$O(\sqrt n)$的判断,因为n比较大所以超时. 想到线性筛,然而我们并不能筛 ...
- shell 练习题
1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 read -p "Please Input A File: " file if [ ! -e ...
- Java使用ResourceBundle类读取properties文件中文乱码的解决方案
Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...
- Java-basic-4-数据类型
Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...
- selenium2基本控件介绍及其代码
输入框:input 表现形式: 1.在html中一般为:<input id="user" type="text"> 主要操作: ...
- loj2014 「SCOI2016」萌萌哒
神tm st表+并查集 #include <iostream> #include <cstdio> #include <cmath> using namespace ...