题目链接: 传送门

Sum of Consecutive Prime Numbers

  1. Time Limit: 1000MS     Memory Limit: 65536K

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. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int MAX = 10005;
  7. int prime[MAX];
  8. bool is_prime[MAX];
  9. int main()
  10. {
  11. int p = 0;
  12. memset(is_prime,true,sizeof(is_prime));
  13. is_prime[0] = is_prime[1] = false;
  14. for (int i = 2;i <= MAX;i++)
  15. {
  16. if (is_prime[i])
  17. {
  18. prime[p++] = i;
  19. for (int j = 2*i;j <= MAX;j += i)
  20. {
  21. is_prime[j] = false;
  22. }
  23. }
  24. }
  25. int N;
  26. while (~scanf("%d",&N) && N)
  27. {
  28. bool flag = false;
  29. int cnt = 0,s = 0,t = 0,sum = 0;
  30. for (;;)
  31. {
  32. if (prime[t] >= N) break;
  33. while (sum < N)
  34. {
  35. sum += prime[t++];
  36. if (sum == N)
  37. {
  38. flag = true;
  39. }
  40. }
  41. if (flag)
  42. {
  43. cnt++;
  44. flag = false;
  45. }
  46. sum -= prime[s++];
  47. if (sum == N && prime[t] < N)
  48. {
  49. cnt++;
  50. sum -= prime[s++];
  51. }
  52. }
  53. if (is_prime[N])printf("%d\n",cnt+1);
  54. else if (cnt)printf("%d\n",cnt);
  55. else printf("0\n");
  56. }
  57. return 0;
  58. }

POJ 2739 Sum of Consecutive Prime Numbers(尺取法)的更多相关文章

  1. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  2. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  6. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

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

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

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. hadoop家族之pig入门

    昨天成功运行第一个在hadoop集群上面的python版本的wordcount,今天白天继续看网上提供的文档.下午上头给定的回复是把hadoop家族都熟悉一下,那就恭敬不如从命,开始学习pig吧- 这 ...

  2. deep learning 的综述

    从13年11月初开始接触DL,奈何boss忙or 各种问题,对DL理解没有CSDN大神 比如 zouxy09等 深刻,主要是自己觉得没啥进展,感觉荒废时日(丢脸啊,这么久....)开始开文,即为记录自 ...

  3. android animation中的参数interpolator详解

      android:interpolator interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果可以 accelerated(加速),decelerated(减速), ...

  4. Android浮动小球与开机自启动

    看着手机上的360浮动小球,不评价其具体的功能与实用性,至少在UI设计与交互方面是个不小的创新. 如图片左上角所示,球中还会显示当前手机的运行状况,向下拉动还会有弹射来达到加速.清理等目的. 那好,先 ...

  5. swfupload提示“错误302”的解决方法

    1.关于图片上传控件,flash控件的显示效果要好一些,本人使用swfupload 2.swfupload上传控件使用方式详见文档 http://www.leeon.me/upload/other/s ...

  6. Ubuntu Terminal Shortcut

    Not all of the shortcuts are useful.Only remeber the most useful. 移动类Ctrl + a  - Jump to the start o ...

  7. Win7 64bit下32bit的 ODBC 数据源问题

    win764位有数据源,但是如果我们在win7 64bit中使用32位的数据源的时候,我们就需要对其进行配置,很有趣的是,64为的数据源我们可以在控制面板——系统与安全——管理工具——数据源,进入可对 ...

  8. Map集合的应用及其遍历方式

    ---> HashMap :底层基于哈希表      存储原理也使用哈希表来存放的:            往HashMap添加了元素 ,首先会调用键的hashCode方法 获得一个哈希值,然后 ...

  9. 模拟发送http请求

    1.httpie 2.postman:Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 3.fiddler

  10. 在Eclipse中生成接口的JUnit测试类

    在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...