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

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
  9.  
  10. 思路:打表,不管是dp还是简单的列举首尾端点
    问题:看错题意以为3 5 5 7 是允许的方式
  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. int method[10001][1300];
  5. int dp[10001];
  6. bool isntprime[10001];
  7. int heap[1300],cnt;
  8. void calprime(){
  9. method[2][0]=1;
  10. dp[2]++;
  11. heap[cnt++]=2;
  12. for(int i=3;i<10001;i+=2){
  13. if(!isntprime[i]){
  14. heap[cnt]=i;
  15. method[i][cnt++]=1;dp[i]++;
  16. for(int j=3;i*j<10001;j+=2){
  17. isntprime[i*j]=true;
  18. }
  19. }
  20. }
  21. }
  22. void caldp(){
  23. for(int i=2;i<10001;i++){
  24. for(int j=0;j<cnt;j++){
  25. if(method[i][j]!=0){
  26. if(j<cnt-1&&i+heap[j+1]<10001){
  27. method[i+heap[j+1]][j+1]+=method[i][j];
  28. dp[i+heap[j+1]]+=method[i][j];
  29. }
  30. }
  31. }
  32. }
  33. }
  34. int main(){
  35. calprime();
  36. caldp();
  37. int n;
  38. while(scanf("%d",&n)==1&&n){
  39. printf("%d\n",dp[n]);
  40. }
  41. return 0;
  42. }

  

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0的更多相关文章

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

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

  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( *【素数存表】+暴力枚举 )

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

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

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

  5. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

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

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

  8. poj 2739 Sum of Consecutive Prime Numbers 小结

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

  9. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

随机推荐

  1. java.lang.OutOfMemoryError: PermGen space异常及解决

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...

  2. 超级强大的vim配置(vimplus)--续集

    An automatic configuration program for vim 安装(github地址:https://github.com/chxuan/vimplus.git, 欢迎star ...

  3. qq第三方登录网站接口

    网站如何实现QQ登录功能 | 浏览:11029 | 更新:2013-12-05 10:09 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅为你的电脑系统,选一个靠谱师傅! 如果想让网站实现 ...

  4. 2017年4月16日 一周AnswerOpenCV佳作赏析

    2017年4月16日 一周AnswerOpenCV佳作赏析 1.HelloHow to smooth edge of text in binary image, based on threshold. ...

  5. openwrt的编译系统是如何制作根文件系统的

    答:分析以下makefile即可获取整个过程 以nxp layerscape系统的编译过程为例 1.分析target/linux/layerscape/image/Makefile的最后一句,这是一个 ...

  6. rhel7配置samba_4.7.1,共享给所有人以及共享给指定用户

    1.共享给所有人 服务端配置: yum  -y install samba samba-client samba-common       #安装客户端 mkdir /guest #创建共享文件夹 c ...

  7. .Net Core Cookie跨站点共享 会话保持

    这里使用简单粗暴的方式,只为做个记录. 关键配置: services.AddDataProtection() .SetApplicationName("appname") .Dis ...

  8. 【cs231n】神经网络学习笔记3

    + mu) * v # 位置更新变了形式 对于NAG(Nesterov's Accelerated Momentum)的来源和数学公式推导,我们推荐以下的拓展阅读: Yoshua Bengio的Adv ...

  9. Centos中使用Jenkins执行gulp命令:command not found

    在Centos操作系统,使用Jenkins的pipeline执行发布流程:jenkinsfile如下: stage("前端项目构架gulp") { steps { dir('src ...

  10. jmeter-负载

    主: remote_hosts=10.0.70.35:1099,10.0.70.47:1099 server.rmi.localport=1099 从:  remote_hosts=10.0.70.3 ...