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. 快速切题 poj 1003 hangover 数学观察 难度:0

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103896   Accepted: 50542 Descr ...

  2. matlab cvx工具包安装

    cvx是凸函数优化的工具包 官网下载地址,http://cvxr.com/cvx/download/ 1 解压到任意文件,最好不要是matlab中的toolbox, 2 假如你解压倒了c盘sample ...

  3. CSS3 文本超出后显示省略号...

    纯用CSS实现,主要采用代码 overflow:hidden; text-overflow:ellipsis;//这是让文本溢出后,显示成省略号. white-space:nowrap;//禁止自动换 ...

  4. Python3基本数据类型(五)

    Python中的变量不需要声明,每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在Python中变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型 ...

  5. d3.js(v5.7)完整地画一个柱状图

    一.首先定义画布大小以及绘画区域的位置(总不能顶着屏幕边沿画吧) 代码: 图示: 二.横.纵向坐标轴 代码: 图示: 三.添加矩形个文本以及上色 图示:

  6. Shell 命令行求两个文件每行对比的相同内容

    Shell 命令行求两个文件每行对比的相同内容 遇到的一个实际问题是,2017年08月01日起,所有未经实名的域名,全部停止解析.而我手上有不少域名,其中很多都是没有实名的.但我不知道哪些实名了,哪些 ...

  7. Cloudify介绍

    一篇关于Cloudify的介绍,可以看一下:http://timeson.iteye.com/blog/1699730

  8. test20190305

    上午考试,是 \(SCOI\ 2016\ Day\ 1\) 的题目. 背单词 这题我以前是做过的.Trie树总结. #include<bits/stdc++.h> using namesp ...

  9. 使用python处理selenium中的frame切换问题

    # iframe有name或id值 self.driver.switch_to.frame('iframe-name-id') # iframe没有name或id值 xf = self.driver. ...

  10. python3.x 类和对象

    python同样支持类和对象.在python3.x中没有了经典类,只有新式类,默认创建的就是新式类.具体的新式类与经典类的不同在python3.x中已经没有意义了. 类的定义和实例化 python定义 ...