POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?

id=2739

题意:

给你一个10000以内的自然数X。然后问你这个数x有多少种方式能由连续的素数相加得来?

分析:

首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中。

然后找到数X在prime中的上界, 假设存在连续的素数之和==X, 那么一定是从一个比X小的素数開始求和(不会超过X的上界),直到和sum的值>=X为止。

所以我们暴力枚举10000以内的全部可能的素数相加和的起始点i,然后求连续素数的和。看看当前以prime[i]開始的连续素数和是否正好==X。

因为10000以内的素数非常少(仅仅有1000多个),所以本题找连续素数和能够暴力枚举解决。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=10000; //筛选法求素数
int prime[maxn+5];
int get_prime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;i++)
{
if(!prime[i]) prime[++prime[0]]=i;
for(int j=1; j<=prime[0] &&prime[j]<=maxn/i; j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0) break;
}
}
return prime[0];
} int main()
{
//预处理:求10000以内全部素数
get_prime(); int x;
while(scanf("%d",&x)==1 && x)
{
if(x<2)
{
printf("0\n");
continue;
}
int bound=lower_bound(prime+1,prime+prime[0]+1,x)-prime;
int ans=0;
if(prime[bound]==x) ans++;
for(int i=1;i<bound;i++)
{
int sum=0;
for(int j=i;j<bound;j++)
{
sum += prime[j];
if(sum==x)
{
ans++;
break;
}
}
}
printf("%d\n",ans);
}
return 0;
}

POJ 2739 Sum of Consecutive Prime Numbers(素数)的更多相关文章

  1. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  2. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  3. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  4. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  7. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  8. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

  9. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

随机推荐

  1. 设置PATH 环境变量、pyw格式、命令行运行python程序与多重剪贴板

    pyw格式简介: 与py类似,我认为他们俩卫衣的不同就是前者运行时候不显示终端窗口,后者显示 命令行运行python程序: 在我学习python的过程中我通常使用IDLE来运行程序,这一步骤太过繁琐( ...

  2. python课堂知识的几点总结

    1.执行python脚本的两种方式:利用python进入解释器和找到可执行文件1.py 2.位和字节:一个字节是8位,计算机运行时是以字节为单位,存储的时候是以位为单位 3.编码发展:ASCII只有0 ...

  3. css预编译器——Less的使用

      方法一:仅介绍在客户端环境下使用的方法 1 新建test.less并引入.less该文件(和css一样在head处引入),注意rel="stylesheet/less": &l ...

  4. LaTeX 基本的公式符号命令

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50240237 下面列出一些基本的LaT ...

  5. Ubuntu PostgreSql主从切换

    主机:192.168.100.70 从机:192.168.100.71 通用配置(即主从都要配置) 修改/etc/postgresql/10/main/pg_hba.conf host all all ...

  6. HDU 4372

    想了很久,终于想到了.... 向后看到F,向前看到B,假如把N-1个楼分成F+B个组,则把每个组最高的楼作为看到的楼,那么,其实在确定每一组的最高楼时,左边或右边的最高楼的顺序已经确定了.由于是排列数 ...

  7. FPGA实现网络通信时的网络字节序问题

    在上位机软件发送字符abcd 在鲨鱼上抓包 用逻辑分析仪从FPGA网络接收管脚分析 数据接收后存储在位宽为8bit的ram中 从ram中读32bitUDP数据为 64636261 依据以上那个现象, ...

  8. 纯文本中识别URI地址并转换成HTML

    问题 有一段纯文本text, 欲将其插入DOM节点div中. text中可能有超链接, 邮件地址等. 假设有, 识别之. 分析 假设仅仅是纯文本, 插入div中, 仅仅要将div.innerText设 ...

  9. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  10. C++ STL之list具体解释

    list容器是一个双向链表,能够高效地进行插入删除元素. 构造函数 list<Elem> c;//空list list<int> c(3);//创建一个含有三个默认值是0的元素 ...