Sum of Consecutive Prime Numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21613   Accepted: 11837

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
 
心得:第一个循环a写成了n;T半天;选择循环时优先选择短的那条;
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define N 10000
int n,m,pr[N],a=;
bool p[N];
void gcd(){
for(int i=;i<N;i++){
if(!p[i]){ pr[a++]=i;}
for(int j=i*i;j<N;j+=i)
p[j]=true;
}
} int main(){
//freopen("in.txt","r",stdin);
memset(p,false,sizeof(p));
gcd();
while(cin>>n,n){
int j=,sum;m=;
for(int i=;i<a;i++){
sum=;j=i;
while(sum<n){
sum+=pr[j++];
}
if(sum==n)
m++;
}
printf("%d\n",m);
}
return ;
}

poj 2739(筛法求素数)的更多相关文章

  1. POJ 2689 筛法求素数

    DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...

  2. poj 2262 筛法求素数(巧妙利用数组下标!)

    Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41582   Accepted: ...

  3. 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  4. hdu 4548 筛法求素数 打表

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...

  5. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  6. 筛法求素数Java

    输出:一个集合S,表示1~n以内所有的素数 import java.util.Scanner; public class 筛法求素数 { public static void main(String[ ...

  7. Algorithm --> 筛法求素数

    一般的线性筛法 genPrime和genPrime2是筛法求素数的两种实现,一个思路,表示方法不同而已. #include<iostream> #include<math.h> ...

  8. JD 题目1040:Prime Number (筛法求素数)

    OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...

  9. 2018牛客网暑期ACM多校训练营(第三场) H - Diff-prime Pairs - [欧拉筛法求素数]

    题目链接:https://www.nowcoder.com/acm/contest/141/H 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  10. 埃氏筛法求素数&构造素数表求素数

    埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...

随机推荐

  1. Redis .net 客户端 分布式锁

    关于Redis分布式锁的参考链接:http://redis.io/topics/distlock. 在我们项目中,之前琢磨用:ServiceStack.Redis,发现ServiceStack.Red ...

  2. tomcat 启动报错 Cannot allocate memory

    Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=256m; support was removed in 8.0 ...

  3. 【bzoj1594-猜数游戏】线段树

    题解: 矛盾只有两种情况: 一.先前确定了x在区间(l,r),但是现在发现x在区间(l1,r1),并且两个区间不相交. 二.一个区间的最小值是x,这个区间中有一个子区间的最小值比x更小. 首先可以明确 ...

  4. 【BZOJ4516】【SDOI2016】生成魔咒 [SAM]

    生成魔咒 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔咒串由许多魔咒字符组成,魔咒字符 ...

  5. px,em,rem字体单位

    1.px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.(引自CSS2.0手册) 2.em是相对长度单位.相对于当前对象内文本的字体尺寸,em存在值继承问题. 浏览器的默认字 ...

  6. Lucene7.2.1系列(一)快速入门

    系列文章: Lucene系列(一)快速入门 Lucene系列(二)luke使用及索引文档的基本操作 Lucene系列(三)查询及高亮 Lucene是什么? Lucene在维基百科的定义 Lucene是 ...

  7. 概率DP入门学习QAQ

    emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...

  8. perl中的lock

    #!/usr/bin/env perl -w use strict; use threads; use threads::shared; ; print "count的起始值为:$count ...

  9. HashMap 、LinkedHashMap、HashTable、TreeMap 和 Properties 的区别

    HashMap 1.线程不安全: 2.允许null value 和 null key: 3.访问效率比较高: 4.Java1.2引进的Map接口的一个实现: 5.轻量级: 6.根据键的HashCode ...

  10. Jmeter跨线程组传递变量

    请求API需要授权令牌,但是授权令牌只需要获取一次,即可调用服务器上其他业务接口. 所以我想要把授权操作放在单独的一个线程,业务流放在其他线程. 这就需要我把从授权线程获取的令牌传入业务流线程. 解决 ...