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

  1. 2
  2. 3
  3. 17
  4. 41
  5. 20
  6. 666
  7. 12
  8. 53
  9. 0

Sample Output

  1. 1
  2. 1
  3. 2
  4. 3
  5. 0
  6. 0
  7. 1
  8. 2
    思路:尺取法操作连续子序列。
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. Scanner in = new Scanner(System.in);
  6. final int MAXN = 10005;
  7. int[] prime = new int[MAXN];
  8. boolean[] isPrime = new boolean[MAXN];
  9. int[] sum = new int[MAXN];
  10. int total;
  11. void table() {
  12. Arrays.fill(isPrime, true);
  13. isPrime[0] = false;
  14. isPrime[1] = false;
  15. for(int i = 2; i < MAXN; i++) {
  16. if(isPrime[i]) {
  17. prime[total++] = i;
  18. for(int j = i + i; j < MAXN; j += i) {
  19. isPrime[j] = false;
  20. }
  21. }
  22. }
  23. sum[0] = 0;
  24. for(int i = 1; i < total; i++) {
  25. sum[i] = sum[i-1] + prime[i-1];
  26. }
  27. }
  28. Main() {
  29. int n;
  30. table();
  31. while((n = in.nextInt()) != 0) {
  32. int res = 0, sum = 0;
  33. int front = 0, rear = 0;
  34. while(true) {
  35. while(rear < total && prime[rear] <= n && sum < n) {
  36. sum += prime[rear++];
  37. }
  38. if(sum == n) {
  39. res++;
  40. }
  41. sum -= prime[front++];
  42. if(front >= total || front > rear) {
  43. break;
  44. }
  45. }
  46. System.out.println(res);
  47. }
  48. }
  49. public static void main(String[] args) {
  50.  
  51. new Main();
  52. }
  53. }

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. vue基础指令

  2. bzoj2929

    题解: 网络流裸题 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<c ...

  3. 重温HTML

    1 <h1> </h1>标题标签 <p> </p>段落标签 <img src=“ ”>图片标签 2. <em>和<stro ...

  4. Flask-WTF表单

    Web表单 Web 表单是 Web 应用程序的基本功能. 它是HTML页面中负责数据采集的部件.表单有三个部分组成:表单标签.表单域.表单按钮.表单允许用户输入数据,负责HTML页面数据采集,通过表单 ...

  5. core文件介绍

    原文链接:http://team.eyou.com/?p=27 如有侵犯您的版权,请联系windeal12@qq.com linux下,产生core文件,和不产生core文件的条件: 当我们的程序崩溃 ...

  6. 自己定义的Excetpion继承哪个异常有什么讲究?[待解答]

    try catch的地方需要用到一个自定义的DBException,如下: 于是我就自定义了一个DBException,继承Excetpion,以父类Exception构造器创建构造器: DBExce ...

  7. linux下c语言源码编译

    一.源码编译过程   源码  ---> 预处理 ---> 编译 ---> 汇编 ---> 链接 --->执行    我们可以把它分为三部分来完成: ./configure ...

  8. 数据库连接池----Druid配置详解

    什么是连接池? 数据库连接池出现的原因在数据库连接资源的低效管理,使用数据库连接池是基于设计模式中的资源池的概念,从而解决资源频繁是分配.释放所造成的问题. 数据库连接池的基本思想就是为数据库连接建立 ...

  9. 一段处理json的C#代码

    服务器端: using Newtonsoft.Json; using Newtonsoft.Json.Linq; public ActionResult GetGatherData() { IList ...

  10. Win10 64bit下安装GPU版Tensorflow+Keras

    Tensorflow和Keras都是支持Python接口的,所以本文中说的都是搭建一个Python的深度学习环境. Keras是对Tensorflow或者Theano的再次封装,也就是以Tensorf ...