题意:

一些正整数可以由一个或多个连续质数的总和表示。给定一个的正整数n,问满足条件的有多少种情况?

题目:

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

分析:

1.将 2 至 10000 内的素数存入一个数组;
2.对于每一个给定的数,从左向右遍历数组,根据连续素数的和的大小不断的增减元素,直到找到一个个解。

AC模板:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e4+10;
int n,k,r,l,ans,mi;
int dp[M],book[M];
void init()
{k=0;/** for(int i=2; i<M; i++){if(!book[i]){dp[k++]=i;for(int j=i*2; j<M; j+=i)book[j]=1;}}*/for(int i=2;i<M;i++){if(!book[i])dp[k++]=i;for(int j=0;j<k&&i*dp[j]<M;j++){book[i*dp[j]]=1;if(i%dp[j]==0)break;}}}
int solve(int x)
{ans=0;for(int i=0; i<k&&dp[i]<=x; i++){l=i,mi=0;while(mi<x&&l<k){mi+=dp[l++];}if(mi==x)ans++;}return ans;
}
int main()
{init();while(~scanf("%d",&n)&&n){printf("%d\n",solve(n));}return 0;
}

备战ccpc分站赛ing ,题目分析简略,见谅,转载请注明出处。。。。。

Sum of Consecutive Prime Numbers POJ - 2739(线性欧拉筛+尺取法)相关推荐

  1. BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...

  2. poj 2739 Sum of Consecutive Prime Numbers

    悲剧,如此水题,竟搞了许久.唉,只因题目没看清. 准确的说:应该是准备工作没做好就开始敲代码了!! 下次一定要想的很清楚了再code ing ! //144K 63MS #include <st ...

  3. UVA 1210 Sum of Consecutive Prime Numbers

    https://vjudge.net/problem/UVA-1210 统计质数前缀和,枚举左右端点,这一段的区间和+1 #include<cstdio> #define N 10001 ...

  4. Sum of Consecutive Prime Numbers

    我们都知道数字是个好玩意,那么我们想知道一个数字能是否能用若干个(或许是一个)连续的素数之和表示,并且想知道有多少种方法.例如,53 有两种表示方法 5 + 7 + 11 + 13 + 17 和 53 ...

  5. 埃氏筛 线性筛(欧拉筛) 算法解析

    埃氏晒 埃拉托斯特尼筛法,简称埃氏晒,是一种用来求自然数n以内的全部素数. 他的基本原理是,如果我们要获得小于n的所有素数,那就把不大于根号n的所有素数的倍数剔除. 埃氏晒的原理很容易理解,一个合数, ...

  6. 素数的线性筛法java,埃氏筛 线性筛(欧拉筛) 算法解析

    埃氏晒 埃拉托斯特尼筛法,简称埃氏晒,是一种用来求自然数n以内的全部素数. 他的基本原理是,如果我们要获得小于n的所有素数,那就把不大于根号n的所有素数的倍数剔除. 埃氏晒的原理很容易理解,一个合数, ...

  7. 2404 Super Prime(欧拉筛素数)

    2404 Super Prime(欧拉筛素数) Problem Description We all know, prime is a kind of special number which has ...

  8. 埃氏筛与欧拉筛(线性筛)

    目录 一.前言 二.埃氏筛与欧拉筛(线性筛) 1.问题描述 2.基本思路 (1)埃氏筛法 (2)欧拉筛法 三.题例 1.上链接 2.简单思路 3.代码 (1)埃氏筛python版 (2)欧拉筛pyth ...

  9. 线性筛(欧拉筛)——算法解析

    算法简介: 欧拉筛是一个能够做到O(n)O(n)O(n)的时间复杂度,也就是线性的质数筛法,是目前性能最优秀的质数筛法.在很多算法和数据结构题目中都有大量的应用,是一个十分基础的工具.对于一个频繁使用 ...

最新文章

  1. 铜陵新松工业机器人项目_投资10亿元,茶山德威工业机器人和精密模具项目动工...
  2. python使用socket实现协议TCP长连接框架
  3. mysql 5.74安装教程_MySQL安装、基本账户安全(5.0以后版本)
  4. 102.二叉树的层序遍历
  5. MTK6763平台手机 在国外无法成功注册VOLTE。求解
  6. 这5个开源和免费静态代码分析工具,你一个都没有用过吗?不会吧
  7. threejs写的模仿微信跳一跳游戏
  8. 分享一款上班摸鱼神器,再也不怕领导突然出现在身后了~
  9. VR全景为家装赋能,沉浸式体验家装设计效果
  10. 美团和支付宝,越来越像
  11. 统俗讲义之——何为统计显著性(Statistical Significance)
  12. unity木船模型,古代帆船模型,古船
  13. php empty 和空字符串区别
  14. 【每日新闻】摩托罗拉折叠屏手机准备就绪:上下折叠
  15. strace命令使用方式
  16. Splunk 常见问题解答
  17. 每日词根——viv(生命)
  18. 阿里P8专家年薪170万IT男征婚被嘲讽?钱不是万能的!
  19. 百度AI使用方案:黑白图片上色
  20. 蓝桥杯 DFS经典题 —— 算式900、寒假作业(告别枚举法)

热门文章

  1. C++之explicit关键字使用总结
  2. oracle内外链接混合用,混合在一起通过连接,内部连接和总结与Oracle
  3. c语言入门数组,C语言入门之数组(2)
  4. php post授权编写,php模拟post行为代码总结(POST方式不是绝对安全)
  5. 面对女人的喋喋不休时,男人内心的真实想法
  6. 延迟开学?这些教育读书公众号可以帮助孩子学习! 你都关注了吗?
  7. 颠覆传统,仅银行卡大小充电宝,10000mAh可登机,手机党出门随身必备!
  8. 干货整理:处理不平衡数据的技巧总结!收好不谢
  9. mysql dw解决方案_MySQL 的 DW 解决方案(MySQL + Infobright)
  10. linux 消息队列_Linux进程间通信第六讲 标准IPC之消息队列