题意:

给出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. 35 个必须有的Bootstrap工具和生成器

    Bootstraptor If you think that bootstrap templates are not enough for you, you should go with bootst ...

  2. Android UI学习1:控件和基本事件的响应

    在任何一个 GUI 系统中,控制界面上的控件(通常称为控件)都是一个基本的内容.对于 Android 应用程序,控件称为 View. 在 Android 中,在处理 UI 中的各种元素的时候,两个程序 ...

  3. android include中的控件调用

    项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...

  4. Fedora 17下安装Oracle 10g详细图文教程

    一.硬件要求——内存 & swap & 硬盘 最小内存与swap: 1 GB of RAM & swap 建议内存与swap: 2 GB of RAM & swap [ ...

  5. 【EF】疑难杂症

    用户和购物车数据  主从表 添加 ADO.NET实体数据类型 [Test] public void EntiyConnect() { var context = new projectDatabase ...

  6. WP8.1和Win8.1的不同之处

    本文仅是个人见解,如有不足或错误之处欢迎批评指正~ 1.Toast: 创建Toast代码差不多但实现机制及管理上不一样 2.ApplicationData: WP8.1多了一个LocalCacheFo ...

  7. mvc权限,登陆,异常

    public class FilterOfPer : ActionFilterAttribute     {         public override void OnActionExecutin ...

  8. xcode7 app loader error itms 90168

    . $ cd ~/.itmstransporter   . $ rm update_check*   . $ mv softwaresupport softwaresupport.bak   . $  ...

  9. CoInitialize()、CoInitializeEx()和AfxOleInit()区别联系

    CoInitialize()和AfxOleInit() 都是初始化COM库,不同之处在与: OLE是建立在COM之上的技术,层次比COM要高.AfxOleInit()调用的是OleInitialize ...

  10. MS Translator

    在看白老师的书的时候看到的,现在MS已经开始转向服务,真对不同行业具有不同的服务,有些免费的,还是十分值得我们借用的,毕竟是大公司出来的产品,都会保证SLA的. 不多说了,直接上地址: https:/ ...