【POJ-2739】Sum of Consecutive Prime Numbers
来源:互联网 发布:mac哪里下载破解软件 编辑:程序博客网 时间:2024/06/01 07:27
Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15369 Accepted: 8525
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.
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
Source
Japan 2005
由于每组都要计算素数,因此可以先采用离线计算的方法,预先处理处10000内的素数表,升序放在prime[X]中,
双重循环,依次枚举素数累加。
#include <stdio.h>int prime[10000],tot=0;int check(int x){int i,j;for(i=0;i<tot;i++)if (x % prime[i]==0) return 0;return 1;}int main(){int i,j;int num,ans,sum; // freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);for(i=2;i<10010;i++)if (check(i)==1) prime[tot++]=i; // for(i=0;i<tot;i++) printf("%d ",prime[i]);scanf("%d",&num);while(num){ans=0;for(i=0;prime[i]<=num;i++){sum=0;for(j=i;j<tot&&sum<num;j++)sum+=prime[j];if (sum==num) ++ans;}printf("%d\n",ans);scanf("%d",&num);}}
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj - 2739 - Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- 【POJ-2739】Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- Java volatile 和 transient 关键字
- lucene索引结构分析
- 程序员修炼,不错的一篇文章
- linux常用命令
- Linux驱动开发--S3C6410GPIO的配置
- 【POJ-2739】Sum of Consecutive Prime Numbers
- 在Ubuntu下进行MongoDB安装步骤
- 单例模式
- android 音频系统
- 关于TweenLite
- UVA 712 S-Trees
- Solution for fixing AudioRecord: obtainBuffer timed out
- 【Android 开发教程】使用静态资源
- Objective C Runtime的一些笔记