POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】
Memory Limit: 65536K
Accepted: 10619
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
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
Source
Japan 2005
题目大意:
一个数能够由若干种连续的素数序列求和得到,比方说41 = 2+3+5+7+11+13 = 11+13+17 = 41
共同拥有三种不同的素数序列求和得到。给你一个数N,求满足N = 连续的素数序列和的方案数
思路:
非常easy的题目。可是用普通方法推断素数可能会超时,这里用了筛法求素数的方法直接用数组Prime
推断是否为素数,另开一个数组PrimeNum用来存全部的素数。
最后就是枚举,求得满足的方案数
#include<stdio.h>
#include<string.h> int Prime[10010],PrimeNum[10010]; int IsPrime()//筛法求素数
{
Prime[0] = Prime[1] = 0; for(int i = 2; i <= 10000; i++)
Prime[i] = 1;
for(int i = 2; i <= 10000; i++)
{
for(int j = i+i; j <= 10000; j+=i)
Prime[j] = 0;
}
int num = 0;
for(int i = 0; i <= 10000; i++)
if(Prime[i])
PrimeNum[num++] = i; return num;
}
int main()
{
int num = IsPrime();
int N;
while(~scanf("%d",&N) && N!=0)
{
int count = 0;
for(int i = 0; PrimeNum[i]<=N && i < num; i++)//枚举
{
int sum = 0;
for(int j = i; PrimeNum[j]<=N && j < num; j++)
{
sum += PrimeNum[j];
if(sum == N)
{
count++;
break;
}
if(sum > N)
break;
}
} printf("%d\n",count);
} return 0;
}
POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】的更多相关文章
- How many prime numbers(求素数个数)
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people e ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- JD 题目1040:Prime Number (筛法求素数)
OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
随机推荐
- Myeclipse集成Maven(图文说明)
myeclipse 上安装 Maven3 环境准备: JDK 1.6 Maven 3.2.5 myeclipse 2013 安装 Maven 之前要求先确定你的 JDK 已经安装配置完毕.Maven是 ...
- [51Nod]NOIP2018提高组省一冲奖班模测训练(一)题解
http://www.51nod.com/contest/problemList.html#!contestId=72&randomCode=147206 原题水题大赛.. A.珂朵莉的旅行 ...
- Hadoop学习小结
还在学校的时候,就知道Hadoop的存在了. 2012年在公司实习的时候,买了<Hadoop权威指南第2版>,大致看了下. 今年,抽空也大致喵了几眼. 最大的感悟就是:光看不做,还是不行. ...
- Core Animation 文档翻译—附录A(Layer样貌相关属性动画)
前言 在渲染过程中,核心动画获取Layer的各种属性并以特定的顺序渲染他们.这个顺序决定了Layer的最终的样貌.本节将会阐述通过设置不同的Layer样貌相关属性对应产生的渲染结果. 注意:Mac ...
- BZOJ 4555 [Tjoi2016&Heoi2016]求和 (多项式求逆)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4555 题目大意: 给定 \(S(n,m)\) 表示第二类斯特林数,定义函数 \(f(n ...
- FFmpegh.264解码
- (int)DecodeH264Frames: (unsigned char*)inputBuffer withLength:(int)aLength { ; ; av_init_packet(&a ...
- JS学习笔记 - fgm练习 2-12- 全选反选 判断CheckBox是否选中 &&运算符
练习地址:http://www.fgm.cc/learn/lesson2/12.html 总结: 1. && 运算符,从左向右依次执行,如果遇到 false,就不再继续执行后面的语句 ...
- 【hdu 6000】Wash
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 因为每件衣服都是没有区别的. 只有洗衣机不同会影响洗衣时间. 那么我们把每台洗衣机洗衣的时间一开始都加入到队列中. 比如{2,3,6 ...
- 【Codeforces Round #446 (Div. 2) C】Pride
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...
- 【河南省多校脸萌第六场 A】分班级
[链接]点击打开链接 [题意] 在这里写题意 [题解] 最大的给了最小的,实际上就对应了,最大值减1,最小值加1. 那么二分最后班级人数最小的最大可能是几->temp1; 二分最后班级人数最大的 ...