POJ 2739 Sum of Consecutive Prime Numbers(数学,素数打表)
2016-02-15 01:11
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
题意:给出一个数n,问n可以与几种形式的连续的素数相加相等。
题解:题目给出的范围是10000,先将10000内的素数打表判断出来,然后按序存入一个数组,10000内的素数有1229个。 最后遍历从1~n之间的素数的子区间,判断有多少个区间和与n相等。
代码如下:
刚刚想了一下,查找的部分可以用尺取法写,这样查找的时间复杂度就降到了O(n),尺取法的代码就不写了。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22069 | Accepted: 12079 |
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
23 1741 66620 12 053
Sample Output
11 32 0 20 1
题意:给出一个数n,问n可以与几种形式的连续的素数相加相等。
题解:题目给出的范围是10000,先将10000内的素数打表判断出来,然后按序存入一个数组,10000内的素数有1229个。 最后遍历从1~n之间的素数的子区间,判断有多少个区间和与n相等。
代码如下:
#include<cstdio> #include<cstring> #include<cmath> int a[10010]={1,1},cnt[2000],k; void primetable() { int i,j; for(i=2;i<=100;++i) { if(a[i]) continue; for(j=i*i;j<10010;j+=i) a[j]=1; } k=0; for(i=2;i<=10000;++i) { if(!a[i]) cnt[k++]=i; } } int main() { primetable(); int n,i,j,ans,sum; while(scanf("%d",&n)&&n)//下面的部分,可以尺取法写,不过这里的O(n^2)的算法能AC了 { ans=0; for(i=0;i<k;++i) { sum=0; if(cnt[i]>n) break; for(j=i;;j++) { sum+=cnt[j]; if(sum>n) break; if(sum==n) { ans++; break; } } } printf("%d\n",ans); } return 0; }
刚刚想了一下,查找的部分可以用尺取法写,这样查找的时间复杂度就降到了O(n),尺取法的代码就不写了。
相关文章推荐
- [算法]数组中未出现的最小正整数
- ps一些比较有用的要点
- [20160214]情人节,也要好好钻研学业!
- 消化
- Chromium网页Render Object Tree创建过程分析
- ruby on rails爬坑(三):图片上传及显示
- 【Bootstrap基础学习】01 Bootstrap的CSS
- LightOJ 1213 - Fantasy of a Summation (排列组合+快速幂)
- cf#342-C - K-special Tables-构造
- Android一步步深入理解View(一):LayoutInflater的使用和原理分析
- 前端面试题:写出删除非空数组最后一个元素并使数组长度减一的几种方法
- 配置eclipse下的C语言开发环境
- Android Studio工程中”R“文件的位置
- sockaddr与sockaddr_in的区别
- leetcode - Range Sum Query - Mutable
- 一个难倒 3年 android开发经验 " 工程师 " 的 "bug"
- 帆软报表学习—单元格扩展和父子格
- cf#342-B - War of the Corporations
- 读《大学之路》有感①
- 安卓json开发误区