Sum of Consecutive Prime Numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23931   Accepted: 13044

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
思路:尺取法操作连续子序列。
import java.util.Arrays;
import java.util.Scanner; public class Main {
Scanner in = new Scanner(System.in);
final int MAXN = 10005;
int[] prime = new int[MAXN];
boolean[] isPrime = new boolean[MAXN];
int[] sum = new int[MAXN];
int total;
void table() {
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for(int i = 2; i < MAXN; i++) {
if(isPrime[i]) {
prime[total++] = i;
for(int j = i + i; j < MAXN; j += i) {
isPrime[j] = false;
}
}
}
sum[0] = 0;
for(int i = 1; i < total; i++) {
sum[i] = sum[i-1] + prime[i-1];
}
}
Main() {
int n;
table();
while((n = in.nextInt()) != 0) {
int res = 0, sum = 0;
int front = 0, rear = 0;
while(true) {
while(rear < total && prime[rear] <= n && sum < n) {
sum += prime[rear++];
}
if(sum == n) {
res++;
}
sum -= prime[front++];
if(front >= total || front > rear) {
break;
}
}
System.out.println(res);
}
}
public static void main(String[] args) { new Main();
}
}

POJ2739(尺取法)的更多相关文章

  1. poj2739尺取法+素数筛

    Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...

  2. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  3. poj2739(尺取法+质数筛)

    题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...

  4. POJ 尺取法

    poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...

  5. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  6. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  8. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  9. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

随机推荐

  1. java程序设计基础篇 复习笔记 第五单元

    1. method header: modifier, return value type, method signature(method name, parameter) method body ...

  2. 淘宝TDDL配置以及使用

    此章节具体介绍一下淘宝TDDL具体配置和使用 1. Spring配置文件配置:================spring-mybatis.xml 中配置============= <bean ...

  3. @Transactional的readOnly、timeout

    1.@Transactional的readOnly 在使用@Transactional注解的时候,有一个属性是readOnly,默认值是false readOnly的意思就是当前的方法是只读的,也就是 ...

  4. myeclipes快捷键

    package com.Test02;//alt+shift+s+s 自动创建toString()//ctrl+ shift+ o  自动导包//* alt+ shift +s+o 有参构造//* a ...

  5. (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理

    异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...

  6. C# POST请求 json格式

    /* * url:POST请求地址,例如:url = "http://localhost:35229/ddn/GetPostData"; * postData:json格式的请求报 ...

  7. error c2129:静态函数已声明但未定义

    今天在做一个c函数暴露给lua 时,出现这个问题. 大概代码是这样的, 头文件: #ifndef LEVEL_DESIGNER_H #define LEVEL_DESIGNER_H extern &q ...

  8. python常用模块之json、pickle模块

    python常用模块之json.pickle模块 什么是序列化? 序列化就是把内存里的数据类型转换成字符,以便其能存储到硬盘或者通过网络进行传输,因为硬盘或网络传输时只接受bytes. 为什么要序列化 ...

  9. Resteasy集成Spring

    很简单,都用最新的版本就可以了.之前在网上找的教程都是用resteasy2.x和spring3集成,但是resteasy2.x和spring4是不行的,弄了很久.最后换成最新的resteasy3.x好 ...

  10. test20190320

    全连 \(n\leq 10^6\) ,保证答案在 \(long\ long​\) 范围内. 比较浅显的 \(dp\ ?\) 记 \(f[i]\) 表示考虑前 \(i\) 个音符,其中第 \(i\) 个 ...