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

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

Source

Japan 2005
过的第一段代码,可是后来发现自己事实上是蠢了,因为根本没必要用筛选法挑选素数,以至于我wrong了几次,因为筛选法挑出来的素数在数组中不是连续存在的,如果不再把素数统一赋值到一个连续的数组中的话,我们在后面进行累加的时候就会出现一些困难,因为我们还需要给vis一个循环,让其过滤掉非素数。我wrong了一次后发现了这个问题,于是赋值到一个连续的数组中,当时我选择的是在筛选素数的同时进行赋值,可是wrong了几次,后来发现,在筛选的过程中赋值,isprimer数组中的素数是不完整的,因为筛选法本身就不是全部遍历筛选的!
  1. //memory:744K time:0MS
    #include <iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. using namespace std;
  5. const int MAXN = ;
  6. int main()
  7. {
  8. bool vis[MAXN];
  9. int isprime[MAXN],k=;
  10. memset(vis,,sizeof(vis));
  11. for(int i=;i<(int)sqrt((double)MAXN);i++) //筛选法挑选素数
  12. {
  13. if(!vis[i])
  14. {
  15. for(int j=i*i;j<MAXN;j+=i)
  16. vis[j]=;
  17. }
  18. }
  19. for(int i=;i<MAXN;i++)
  20. {
  21. if(!vis[i])
  22. isprime[k++]=i;
  23. }
  24. int n;
  25. while(cin>>n)
  26. {
  27. if(n==)
  28. break;
  29. //因为不一定是从头开始,所以需要两层循环,
  30. //每一个i都需要从i开始往后累加
  31. int num=;
  32. for(int i=;isprime[i]<=n;i++) //isprime循环
  33. {
  34. int ans = ; //累加器
  35. for(int j=i ; j<k&& ans<n ; j++) //从每一个i开始往后循环
  36. {
  37. ans += isprime[j];
  38. }
  39. if(ans==n)
  40. num++;
  41. }
  42. cout<<num<<endl;
  43. }
  44. return ;
  45. }

以下给出一个较好的代码,挑选素数的想法很好,素数只能被素数整除,不会被偶数整除。但是这种方法很明显慢一些

  1. //memory :736K time :32MS
    #include<iostream>
  2. using namespace std;
  3. const int MAXN = ;
  4. int prime[MAXN],prime_num = ;
  5.  
  6. bool isprime(int n)
  7. {
  8. for(int i=;i<prime_num;i++)
  9. {
  10. if(n%prime[i]==)
  11. return false;
  12. }
  13. return true;
  14. }
  15.  
  16. int main()
  17. {
  18. int n;
  19.  
  20. for(int i=;i<MAXN;i++)
  21. {
  22. if(isprime(i))
  23. {
  24. prime[prime_num++]=i;
  25. }
  26. }
  27. while(cin>>n)
  28. {
  29. if(n==)
  30. break;
  31. int num=;
  32. for(int i=;prime[i]<=n;i++)
  33. {
  34. int ans = ;
  35. for(int j=i;j<prime_num&&ans<n;j++)
  36. {
  37. ans += prime[j];
  38. }
  39. if(ans == n)
  40. num++;
  41. }
  42. cout<<num<<endl;
  43. }
  44. return ;
  45. }

poj2739的更多相关文章

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

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

  2. poj2739 poj2100 尺取法基础(二)

    都是很简单的题目 poj2739素数打表+单点推移 #include<iostream> #include<cstring> #include<cstdio> us ...

  3. POJ2739解题报告

    2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer: ...

  4. POJ2739 - Sum of Consecutive Prime Numbers(素数问题)

    题目大意 给定N,要求你计算用连续的素数的和能够组成N的种数 题解 先筛选出素数,然后暴力判断即可... 代码: #include<iostream> #include<cstrin ...

  5. 【POJ2739】Sum of Consecutive Prime Numbers

    简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...

  6. poj2739尺取法+素数筛

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

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

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

  8. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏

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

  9. POJ2739(尺取法)

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

随机推荐

  1. Python Scrapy安装杂症记录

    昨天安装了scrapy一切正常,调试了bbsSpider案例(详见上文),今日开机因为冰封还原,提示找不到python27.dll,重新安装了python2.7, 使用easy-install scr ...

  2. NOTIFYICONDATA结构

    //农机调度项目代码 NOTIFYICONDATA m_notifyData; m_notifyIcon.ChangeIcon(IDI_PAUSE, _T("监控终端server已暂停&qu ...

  3. mini2440裸机试炼之——DMA直接存取 实现Uart(串口)通信

    这个仅仅能作为自己初步了解MDA的开门篇 实现功能: 将字符串数据通过DMA0通道传递给UTXH0,然后在终端 显示.传输数据完后.DMA0产生中断,beep声, LED亮. DMA基本知识 计算机系 ...

  4. c#基础: 线程的初级用法总结

    启动一个线程的两种方法:     a.使用无参的方法       Thread thread1 = new Thread(new ThreadStart("调用的方法名")):   ...

  5. JS高级程序设计学习笔记之数组

    数组创建的方式 var str = new Array();放入数字即为设置数组长度 var str = []; 数组的length可读可写 监测数组 Array.isArray()方法确定某个值是不 ...

  6. OWIN启动项的检测

    OWIN启动项的检测 通过以下方法设置启动项: 命名约定 Katana在命名空间内查找StartUp类 OwinStartup Attribute [assembly: OwinStartup(typ ...

  7. app打包,发布(同步发生冲突)

    1:打包步骤: 1:桌面建立一个文件夹,名字叫keystore 2:点击build下面的 ,如下:     3:会出现如下界面: 4:下一步: 5:如果有keystore,请点击 choose exi ...

  8. hdu1272并查集入门

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. java-web-dom4j解析XML-递归方式

    <?xml version="1.0" encoding="UTF-8"?><书架>  <书 出版日期="2013-10 ...

  10. web api简单验证实现办法

    需要使用WEBAPI,但是有验证问题没解决.后来参考网上文章做了一下DEMO 思路: 就是根据用户的账号在服务端加密一个字符串,然后返回给用户端. 具体: 一个用户编号用于唯一身份识别,密码,一个密钥 ...