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. struts2实现图片验证码

    生成图片验证码的主要工具类方法为: package com.yeting.fc.util; import java.awt.Color; import java.awt.Font; import ja ...

  2. dubbo Failed to check the status of the service com.user.service.UserService. No provider available for the service

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'u ...

  3. windows server 2008开机自动登陆无密码,关机不必写原因

    运行secpol.sec接下来,在弹出的“本地安全策略”对话框中,依次展开左边树图到“本地策略”-“安全选项”,在右边可以找到“交互式登录 无须按 Ctrl+Alt+Del”,双击该项设置为“已启用” ...

  4. 【BZOJ3270】博物馆 概率DP 高斯消元

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  5. JUnit4.8.2源码分析-4 RunNotifier与RunListener

    JUnit4运行过程中,org.junit.runner.notification. RunListener和RunNotifier运用了观察者模式. 1.观察者 观察者Observer/Listen ...

  6. bzoj1797: [Ahoi2009]Mincut 最小割(最小割+强联通tarjan)

    1797: [Ahoi2009]Mincut 最小割 题目:传送门 题解: 感觉是一道肥肠好的题目. 第二问其实比第一问简单? 用残余网络跑强联通,流量大于0才访问. 那么如果两个点所属的联通分量分别 ...

  7. linux下面增加磁盘空间

    1.先看看情况 [root@localhost tmp]# fdisk -l Disk /dev/sda: 3221 MB, 3221225472 bytes 255 heads, 63 sector ...

  8. vue-cli全引入jquery

    欢迎加入前端交流群交流知识&&获取视频资料:749539640 vue-cli全引入jquery:(vue-cli使用webpack) 第一步: 在package.json文件里的de ...

  9. [jzoj 4722] [NOIP2016提高A组模拟8.21] 跳楼机 解题报告 (spfa+同余)

    题目链接: http://172.16.0.132/senior/#main/show/4722 题目: DJL为了避免成为一只咸鱼,来找srwudi学习压代码的技巧.Srwudi的家是一幢h层的摩天 ...

  10. [TJOI2017] DNA 解题报告 (hash+二分)

    题目链接:https://www.luogu.org/problemnew/show/P3763 题目大意: 给定原串S0,询问S0有多少个子串和给定串S相差不到3个字母 题解: 我们枚举S0的子串, ...