题意:

给出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. Ubuntu系统启动时waiting for network

    最近在使用Ubuntu时启动经常会遇到等待网络配置, 每次等待时间都很长,要几分钟,于是在网上看看其他大牛怎么解决该问题. 有些解决方法中有提到删除 网卡硬件信息文件/etc/udev/rules.d ...

  2. spring mvc 初步接触学习笔记

    1.使用maven导入spring mvc web 的jar 包 最新语句 <dependency> <groupId>org.springframework</grou ...

  3. 【css】web标准

    网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior) 结构重点理解: XHTML 1.应用形式 ccs+div  2000 2.基于xml 和x ...

  4. EXTJS4.2 时间动态刷新显示

    function clockGo() { Ext.TaskManager.start({ run: function () { //Ext.getCmp("clock").setT ...

  5. Eclipse中设置作者日期等信息

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author 的属性值. 我们可以在Eclipse 中进行设置自己希望显示的信息. 现在看看如何修改 ...

  6. dnf的动画脚本研究

    . 1.0x00    : 帧数           int16 2.0x02    : 总为1(?) int16 3.0x04  : 资源文件名长度 int32 4.长度+1 :   0,1(未知用 ...

  7. 请求管道与IHttpModule接口

    IHttpModule向实现类提供模块初始化和处置事件.  IHttpModule包含兩個方法: public void Init(HttpApplication context);public vo ...

  8. 玩转Log4Net

    玩转Log4Net 下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.11 ...

  9. jquery盒模型元素尺寸设置

    jQuery有多个处理元素尺寸的方法 width():设置或返回元素的宽度(不包括padding.border.和margin) height():设置或返回元素的高度(不包括padding.bord ...

  10. c++ deque 双端队列

    双端队列: 函数 描述 c.assign(beg,end)c.assign(n,elem)  将[beg; end)区间中的数据赋值给c.将n个elem的拷贝赋值给c. c.at(idx)  传回索引 ...