POJ 2739 Sum of Consecutive Prime Numbers(素数打表+素数搜索)
2015-10-20 21:41
459 查看
Sum of Consecutive Prime Numbers
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21346 | Accepted: 11695 |
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 题意:将一个数分解为几个素数之和,求出共有几种组合方法,打表深搜 ac代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #define MAXN 2555550 #define mod 1000000007 #define INF 0xfffffff #define MIN(a,b) a>b?b:a using namespace std; int v[MAXN]; int pri[MAXN]; int n,ans,k; void db() { int i,j; for(i=1;i<=10001;i++) v[i]=1; k=0; v[1]=0; for(i=2;i<=10001;i++) { if(v[i]) { for(j=i*2;j<=10001;j+=i) { v[j]=0; } pri[k++]=i; } } } void find(int sum,int num) { if(sum==n) { ans++; return; } if(sum+pri[num]>n) { return; } else { find(sum+pri[num],num+1); } return; } int main() { int i; db(); while(scanf("%d",&n)!=EOF,n) { ans=0; for(i=0;i<k;i++) { find(0,i); } printf("%d\n",ans); } return 0; }
相关文章推荐
- linux下调试core的命令,察看堆栈状态命令
- 悲观锁和乐观锁
- iOS NSData 和十六进制字符串之间的相互转换
- 关于linux环境变量的一些小知识
- HDU 1003 Max Sum
- C++_面向对象程序设计基本概念
- python处理.csv文件
- 函数递归与栈的实用
- Codeblocks注释快捷键
- python画图
- bit 10_12
- Django 入门Demo
- Android之app引导页(背景图片切换加各个页面动画效果)
- 选择,冒泡排序,折半查找,插入
- 选择,冒泡排序,折半查找,插入
- 《软件需求十步走》阅读笔记二
- POJ 3371:Flesch Reading Ease 模拟
- hpux 11.31添加、删除磁盘
- HDU3709 Balanced Number 数位DP
- 面试题39:二叉树的深度