题意:

给出n,求把n写成若干个连续素数之和的方案数。

分析:

这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的优化。

首先筛出10000以内的素数,放到一个数组中,然后求出素数的前缀和B。这样第i个素数一直累加到第j个素数,就可表示为Bj - Bi-1

枚举连续子序列的右端点j,我们要找到Bj - Bi-1 = n,也就是找到Bi-1 = Bj - n。

因为Bj是递增的,所以Bi-1也是递增的,所以我们就不用从头枚举i,而是接着上一次循环i的值继续枚举。

还有就是,因为本身素数也是递增的(废话!),所以j也不一定要枚举到最后一个素数,只要在不超过n的素数里枚举就行了。

说了这么多,我就是想说人家算法的效率已经很高了,15ms,UVa上居然排300+名,鄙视那些打表狗。

 #include <cstdio>
#include <cmath> const int maxn = ;
const int maxp = ;
bool vis[maxn + ];
int prime[maxp], sum[maxp], cnt = ; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; ++i) if(!vis[i])
for(int j = i * i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; ++i) if(!vis[i]) prime[cnt++] = i;
cnt--;
//求素数的前缀和
for(int i = ; i <= cnt; ++i) sum[i] = sum[i - ] + prime[i];
} int main()
{
Init();
int n;
while(scanf("%d", &n) == && n)
{
int i = , ans = ;
for(int j = ;j <= cnt && prime[j] <= n; ++j)
{
int temp = sum[j] - n;
while(sum[i] < temp) ++i;
if(sum[i] == temp) ans++;
}
printf("%d\n", ans);
} return ;
}

代码君

UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers的更多相关文章

  1. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

  3. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  4. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏

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

  5. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers http://poj.org/problem?id=2739 Time Limit: 1000MS   Memory Limit: 6 ...

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

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

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

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

  8. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

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

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

随机推荐

  1. php5 图片验证码一例

    php5 图片验证码. GD库的函数1,imagecreatetruecolor -----创建一个真彩色的图像imagecreatetruecolor(int x_size,int y_size) ...

  2. Linux编程学习笔记 -- Process

    进程是一个程序的运行.   在一个程序中执行另一个执程序的方法有两种: 1)system 在shell中执行程序 2)fork + exec 复制一个进程,在进程中用新的程序替换原有的程序   for ...

  3. [转]java gridbag 说明

    gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight = 1; // 列占一个单元格 weightx = 0.0 ...

  4. 回车,根据编码获取相应记录,然后再将这录绑定到AutoList

    问题描述: 回车后,根据编码获取相应记录,然后再将这录绑定到AutoList(我们自定义控件,其实就是下拉列表),回车事件是用jquery ajax实现,这样在后台给AutoList绑定数据源,如果不 ...

  5. 移动端webapp开发必备知识

    移动设备的用户越来越多,每天android手机的激活量都已经超过130万台,所以我们面向移动终端的WebAPP也开始跟进了.本文主要介绍webapp的开发与调试的相关知识和经验,以及给出几种可选的解决 ...

  6. 【android-cocos2d-X2.2 环境配置】在Mac下搭建Cocos2d-X-android开发环境!

    仅用于cocos2d-X2.2--cocos2d-X3.4 原文地址:http://blog.csdn.net/dingkun520wy/article/details/17097593 (1)下载 ...

  7. github简单使用

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  8. C# DateTime和DateTime?格式化时间

    DateTime: <%= Model.CreateTime.ToString("yyyy年MM月dd日 H时m分s秒")%>   DateTime?: <%= ...

  9. JavaScript中常谈的对象

    为浏览器编写代码时,总少不了window对象 window对象表示JavaScript程序的全局环境 同时 也表示应用的主窗口 到处都是对象 window对象 常用的属性和方法介绍 location ...

  10. <Learning How to Learn>Week One: Focused versus Diffuse Thinking

    1-1 Introduction to the focused and diffuse modes (4:40) 两种思考的模式:focused mode以及diffuse mode focused ...