C - Sum of Consecutive Prime Numbers(1.2.2)
来源:互联网 发布:mysql 派生表 编辑:程序博客网 时间:2024/05/16 11:50
C - Sum of Consecutive Prime Numbers(1.2.2)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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.
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
2317412066612530
Sample Output
11230012#include <iostream>#include <vector>using namespace std;vector <int> prime;int len=0;bool is_prime(int n){ int i; if(n<=1) return false; else if(n==2) return true; else if(n%2==0) return false; else for(i=3;i*i<=n;i+=2) if(n%i==0) return false; return true;}void primes(){ int i; prime.push_back(0); //将第一个数初始化为0,以便用于求输入的数本身是否为素数 for(i=2;i<=10000;i++) if(is_prime(i)) prime.push_back(i); //找出10000以内所有的素数 len = prime.size(); for(i=1;i<len;i++) { prime[i] += prime[i-1]; //利用动态规划,计算出每个素数与其之前所有素数之和 }}int main(){ int Posinteger,count,i,j; primes(); while(cin>>Posinteger&&Posinteger) { count = 0; for(i=0;i<len;i++) { for(j=i+1;j<len;j++) if(prime[j]-prime[i]==Posinteger) //判断前j个素数之和与前i个素数之和的差是否与Posinteger相等, count++; //如果相等,则表明有一组连续的素数相加之和与之相等,符合题意。 } cout<<count<<endl; } return 0;}
0 0
- C - Sum of Consecutive Prime Numbers(1.2.2)
- C - Sum of Consecutive Prime Numbers(1.2.2)
- C - Sum of Consecutive Prime Numbers(1.2.2)
- 《数据结构编程实验》 1.2.2Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers pku 2739
- POJ2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- MySQL-->基础知识-->MySQL 常见 问题汇总
- JAVA WEB里头有关url-pattern的爱恨情仇(搜集了部分人的介绍)
- Unity的吐血巨坑之处 --- 发布版本和编辑器的纹理显示不一致
- Python 之ConfigParser
- 约瑟夫环:递归算法
- C - Sum of Consecutive Prime Numbers(1.2.2)
- 经典、重要文章、帖子、技术个人收藏
- July博客第十二章参考学习
- CSS文档流与块级元素和内联元素
- Linux 标准目录结构
- 字符串匹配KMP算法
- Hdu 1198 Farm Irrigation 基础并查集
- Integer Inquiry
- D - I Think I Need a Houseboat(1.3.1)